2013-03-18 27 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

相關問題