2014-02-07 59 views
0

我想找到一个正则表达式来匹配句子中括号内的任何单词。匹配句子中括号内的任何单词

假设,我有一个句子。

"Welcome, (Hello, All of you) to the Stack Over flow." 

说,如果我匹配的单词是Hello,Allofyou。它应该返回true。 单词可能包含任何数字,符号但与其他空格分开

我试过这个\(([^)]*)\)。但这个方法返回括号

static void Main(string[] args) 
     { 

      string ss = "Welcome, (Hello, All of you) to the Stack Over flow."; 
      Regex _regex = new Regex(@"\(([^)]*)\)"); 
      Match match = _regex.Match(ss.ToLower()); 
      if (match.Success) 
      { 
       ss = match.Groups[0].Value; 
      } 

     } 

帮助和指导是非常赞赏封闭所有单词。

谢谢。

感谢人们为你准备的时间和答案。我终于通过将我的代码更改为Tim的回复来解决问题。 对于有类似问题的人。我在这里写我的最终代码

static void Main(string[] args) 
     { 
      string ss = "Welcome, (Hello, All of you) to the Stack Over flow."; 
      Regex _regex = new Regex(@"[^\s()]+(?=[^()]*\))"); 
      Match match = _regex.Match(ss.ToLower()); 
      while (match.Success) 
      { 
       ss = match.Groups[0].Value; 
       Console.WriteLine(ss); 
       match = match.NextMatch(); 
      } 

     } 
+1

向我们展示您试过的代码。 – user3064914

+0

更新了问题 – Miller

+1

是否可以有(嵌套括号(如此)),如果是,那么这些词中的哪一个应该匹配? –

回答

1

OK,这样看来,一个“字”是什么,这不是空白,并且不包含括号,并且要匹配一个单词如果下一个括号字符下面是一个右括号。

所以,你可以使用

[^\s()]+(?=[^()]*\)) 

说明:

  • [^\s()]+相匹配的 “字”(应该是很容易理解),并
  • (?=[^()]*\))确保了一个右括号如下:

    (?=  # Look ahead to make sure the following regex matches here: 
    [^()]* # Any number of characters except parentheses 
    \)  # followed by a closing parenthesis. 
    )   # (End of lookahead assertion) 
    
+0

这只会匹配括号中包含的单词吗? – Miller

+0

是的(假设所有的括号都是正确的平衡和未发现的)。尝试一下! –

+0

用我的代码和你的正则表达式,它只返回第一个单词。 – Miller

0
(?<=[(])[^)]+(?=[)]) 

匹配括号中的所有单词

(?<=[(])支票(

[^)]+匹配的一切,直到但不包括)

(?=[)])支票)

+0

这将返回文本中的所有文本,与我的问题相同的结果 – Miller

+0

你想要返回什么? – Srb1313711

+0

我一直在努力逐词获取。请看我编辑的问题。 – Miller

1

我为你开发了一个c#函数,如果你有兴趣的话。

public static class WordsHelper 
{ 
    public static List<string> GetWordsInsideParenthesis(string s) 
    { 
     List<int> StartIndices = new List<int>(); 
     var rtn = new List<string>(); 
     var numOfOpen = s.Where(m => m == '(').ToList().Count; 
     var numOfClose = s.Where(m => m == ')').ToList().Count; 
     if (numOfClose == numOfOpen) 
     { 
      for (int i = 0; i < numOfOpen; i++) 
      { 
       int ss = 0, sss = 0; 
       if (StartIndices.Count == 0) 
       { 
        ss = s.IndexOf('(') + 1; StartIndices.Add(ss); 
        sss = s.IndexOf(')'); 
       } 
       else 
       { 
        ss = s.IndexOf('(', StartIndices.Last()) + 1; 
        sss = s.IndexOf(')', ss); 
       } 
       var words = s.Substring(ss, sss - ss).Split(' '); 
       foreach (string ssss in words) 
       { 
        rtn.Add(ssss); 
       } 
      } 
     } 
     return rtn; 
    } 
} 

只是把它这种方式:

var text = "Welcome, (Hello, All of you) to the (Stack Over flow)."; 
      var words = WordsHelper.GetWordsInsideParenthesis(s); 

现在你要在words可变单词的列表。

一般来说,你应该选择c#编码,而不是正则表达式,因为在性能方面c#更加高效和可读,并且比正则表达式更好。

但是,如果你想坚持到正则表达式,那么它的确定,执行以下操作:

如果你想使用正则表达式,保持正则表达式蒂姆Pietzcker [^\s()]+(?=[^()]*\))但使用这种方式:

var text="Welcome, (Hello, All of you) to the (Stack Over flow)."; 
var values= Regex.Matches(text,@"[^\s()]+(?=[^()]*\))"); 

现在values包含MatchCollection

可以使用索引和Value属性

访问值

类似这样的:

string word=values[0].Value; 
+0

谢谢,我正在寻找正则表达式。我已经有了C#解决方案。 – Miller