2014-05-21 196 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

相关问题