C# 具有未知空格数的负向后看
使用c#regex,我试图匹配引号中不在括号中的内容,同时忽略任何空白:C# 具有未知空格数的负向后看,c#,regex,C#,Regex,使用c#regex,我试图匹配引号中不在括号中的内容,同时忽略任何空白: "blah" - match ("blah") - no match ( "blah") - no match ( "blah") - no match 我有(未被替换的): ”(?据我所知,在PCRE中,lookbehind必须是固定宽度。如果这在C#的PCRE引擎中仍然是正确的,那么你将无法按照你尝试的方式来做。据我所知,在PCRE中,lookbehind必须是固定宽度。如果这在C#的PCRE引擎中仍然是正确的,那
"blah" - match
("blah") - no match
( "blah") - no match
( "blah") - no match
我有(未被替换的):
”(?据我所知,在PCRE中,lookbehind必须是固定宽度。如果这在C#的PCRE引擎中仍然是正确的,那么你将无法按照你尝试的方式来做。据我所知,在PCRE中,lookbehind必须是固定宽度。如果这在C#的PCRE引擎中仍然是正确的,那么你将无法按照你尝试的方式来做 这应该有效:
/(?<![^(\s])\s*"([^"]*)"\s*(?![\s)])/
/(?将匹配带引号的字符串,并捕获其内容
\s*
将匹配任意数量的空白字符
最后,(?![\s)]
将声明后面没有空格或右括号
它们一起确保所有空格都与每个\s*
匹配,并且它们不在括号内。这应该可以:
/(?<![^(\s])\s*"([^"]*)"\s*(?![\s)])/
/(?将匹配带引号的字符串,并捕获其内容
\s*
将匹配任意数量的空白字符
最后,(?![\s)]
将声明后面没有空格或右括号
它们一起确保所有空格都与每个\s*
匹配,并且它们不在括号内。向后看需要一个固定的宽度,但您可能可以使用下面的表达式实现。这假设没有嵌套
/\G # from the spot of the last match
(?: # GROUP OF:
[^("]* # anything but open-paren and double quote.
[(] # an open-paren
[^)]* # anything but closing-paren
[)] # a closing-paren
)* # any number of times
[^"]* # anything but double quote
"([^"]*)" # quote, sequence of anything except quote, then ending quote
/x
“向后看”需要一个固定的宽度,但您可能可以使用下面的表达式实现。这假设没有嵌套
/\G # from the spot of the last match
(?: # GROUP OF:
[^("]* # anything but open-paren and double quote.
[(] # an open-paren
[^)]* # anything but closing-paren
[)] # a closing-paren
)* # any number of times
[^"]* # anything but double quote
"([^"]*)" # quote, sequence of anything except quote, then ending quote
/x
不带右括号的“(“blah”如何?不带右括号的“(“blah”如何?不,.NET(正则表达式引擎不特定于C#)支持可变长度look-behinds。不,.NET(正则表达式引擎不特定于C#)不支持可变长度look-behinds。如果引号内有任何结束符,则仍将其视为结束符。如果引号内有任何结束符,则仍将其视为结束符。