2014-05-21 122 views
-2

我需要幫助修復下面的正則表達式。我試圖將它從Python重寫爲C#,但C#顯示空的m.value。謝謝!C#正則表達式匹配整個括號內容

在Python它運作良好,並顯示括號內及內容:

Python代碼:

r1="(dog apple text) (grape cushion cat)" 
a=re.findall("[(]+[/s]+[a-z]+[)]+",r1) 
print(a[:]) 
//Conent gives me (dog apple text) (grape cushion cat) , so if I will call print(a[0]) it will give me (dog apple text) 

String r1="(dog apple text) (grape cushion cat)" 
    String [email protected]"[(]+[/s]+[a-z]+[)]+"; 

     foreach (Match m in Regex.Matches(irregv, pat2)) 
       {     
        Console.WriteLine("'{0}'", m.Value);        
       } 
+2

'/ s'應該是'\ s',但即使這樣你的表達式也會匹配'(((((你好))'而不是你的例子中的任何東西。教程 - http://regular-expressions.info有很多很好的例子。還值得一看[參考 - 這是什麼正則表達式?](http://stackoverflow.com/questions/22937618/reference-what -does-this-regex-mean)在SO上。 – OGHaza

回答

2

你的正則表達式不蟒蛇工作,要麼。

你想使用:

\([a-z\s]+\) 

\(匹配一個開括號,[a-z\s]允許字母(小寫),任何種類的空格通過\s(注意 -slash)。

查看(並參與)demo here

相關問題