2013-03-18 25 views
0

我有一个正则表达式:为什么在Visual Studio中不能反向RegEx匹配?

ConfigurationManager.ConnectionStrings.Item\(\"((?!foo).)*\" 

,在Rubular工作和预期相匹配的两个字符串的第二

ConfigurationManager.ConnectionStrings.Item("foo") 
ConfigurationManager.ConnectionStrings.Item("bar") 

但是,如果我在Visual Studio 2005中运行相同的表达 - 我没有得到任何比赛。它实际上应该匹配存在ConfigurationManager.ConnectionStrings.Item...的每一个实例,因为它们都不匹配foo这个词。

除非在Visual Studio中反向表达式不起作用。

如果这是真的,我将如何去在Visual Studio 2005中获得相同的结果?

+1

我不知道VisualStudio,但[反面查找](http://www.regular-expressions.info/lookaround.html)没有在许多正则表达式引擎中实现。 – 2013-03-18 16:47:48

+0

@dystroy,我想知道。对于实现者来说,我认为这是其中一种情况,所以如果他们能够做到这一点,他们就会完成它。我相信这很复杂。 – 2013-03-18 16:48:58

+0

@dystroy它是在.NET中。实际上 - 在某种程度上 - 在大多数现代正则表达式引擎中。这里使用的固定长度断言几乎得到普遍支持。 /编辑啊,这是关于VS中的搜索和替换功能。然后所有投注都关闭。 – 2013-03-18 16:51:11

回答

2

下面的正则表达式适于从regular expression for find and replace feature in Visual Studio语法,这是不是通常的基于Perl的正则表达式语法

ConfigurationManager.ConnectionStrings.Item\("(~(foo).)*" 

~(pattern),根据描述:

Prevent match ~(X) Prevents a match when X appears at this point 
         in the expression. For example, real~(ity) matches 
         the "real" in "realty" and "really," but not the 
         "real" in "reality." 

应工作方式类似,负前瞻(?!pattern)作品。

+0

梦幻般的答案,这工作完全符合我的预期!我可以在7分钟内做出答复。 – 2013-03-18 16:53:44

+0

@MichaelPerrenoud那7分钟很长... – 2013-03-20 17:12:44

相关问题