这是一个答案:
(?<=^([^"]|"[^"]*")*)text
这表示:
(?<= # preceded by...
^ # the start of the string, then
([^"] # either not a quote character
|"[^"]*" # or a full string
)* # as many times as you want
)
text # then the text
您也可以轻松地将其扩展为处理包含转义的字符串。
在 C# 代码中:
Regex.Match("bla bla bla \"this text is inside a string\"",
"(?<=^([^\"]|\"[^\"]*\")*)text", RegexOptions.ExplicitCapture);
从评论讨论中添加 - 扩展版本(基于每行匹配并处理转义)。用于RegexOptions.Multiline
此:
(?<=^([^"\r\n]|"([^"\\\r\n]|\\.)*")*)text
在 C# 字符串中,这看起来像:
"(?<=^([^\"\r\n]|\"([^\"\\\\\r\n]|\\\\.)*\")*)text"
由于您现在想使用**
而不是"
这里是一个版本:
(?<=^([^*\r\n]|\*(?!\*)|\*\*([^*\\\r\n]|\\.|\*(?!\*))*\*\*)*)text
解释:
(?<= # preceded by
^ # start of line
( # either
[^*\r\n]| # not a star or line break
\*(?!\*)| # or a single star (star not followed by another star)
\*\* # or 2 stars, followed by...
([^*\\\r\n] # either: not a star or a backslash or a linebreak
|\\. # or an escaped char
|\*(?!\*) # or a single star
)* # as many times as you want
\*\* # ended with 2 stars
)* # as many times as you want
)
text # then the text
由于此版本不包含"
字符,因此使用文字字符串更简洁:
@"(?<=^([^*\r\n]|\*(?!\*)|\*\*([^*\\\r\n]|\\.|\*(?!\*))*\*\*)*)text"