正则表达式中找到任何字符()

时间:2013-08-01 02:25:33

标签: c# regex

我有一个很长的文本,在文本中有很多这样的东西(你好,你好)或(你好,你好),我必须考虑到空间。我如何在长文本中检测它们并检索hello和hi单词并从文本中添加到列表中?目前我使用这个正则表达式:

   string helpingWordPattern = "(?<=\\()(.*?)(?<=\\))";
   Regex regexHelpingWord = new Regex(helpingWordPattern);

        foreach (Match m in regexHelpingWord.Matches(lstQuestion.QuestionContent))
       {
           //  removing "," and store helping word into a list
           string str = m.ToString();
           if (str.Contains(","))
           {
                string[] strWords = str.Split(','); // Will contain a ) with a word , e.g. ( whole) ) 

               if(strWords.Contains(")")) 
               {
                   strWords.Replace(")", ""); // Try to remove them. ERROR here cos i can't use array with replace.
               }

                   foreach (string words in strWords)
                   {
                       options.Add(words);
                   }

           }
       }

我google并搜索正确的正则表达式,我使用的正则表达式也可以删除)但它没有。

3 个答案:

答案 0 :(得分:3)

\\( \\)括号匹配器放在您要捕获的组之外?

Regex regex = new Regex( "\\((.*?)\\)");
foreach (Match m in regex.Matches( longText)) {
    string inside = Match.Groups[1];  // inside the brackets.
    ...
}

然后使用Match.Groups[1],而不是匹配的全文。

答案 1 :(得分:2)

有很多不同的方法可以做到这一点......下面是一些使用正则表达式进行匹配/拆分的代码。


string input = "txt ( apple , orange) txt txt txt ( hello, hi,5 ) txt txt txt txt";

List Options = new List();

Regex regexHelpingWord = new Regex(@"\((.+?)\)");

foreach (Match m in regexHelpingWord.Matches(input))
{

    string words = Regex.Replace(m.ToString(), @"[()]", "");

    Regex regexSplitComma = new Regex(@"\s*,\s*");


    foreach (string word in regexSplitComma.Split(words))
    {
        string Str = word.Trim();
        double Num;
        bool isNum = double.TryParse(Str, out Num);
        if (!isNum) Options.Add(Str);
    }

}

答案 2 :(得分:2)

您也可以使用此正则表达式模式:

(?<=[\(,])(.*?)(?=[\),])
(?<=[\(,])(\D*?)(?=[\),])  // for anything except number

分手:

(?<=[\(,])  = Positive look behind, looks for `(`or `,` 
(.*?)       = Looks for any thing except new line, but its lazy(matches as less as possible)  
(?=[\),])   = Positive look ahead, looks for `)` or `,` after `hello` or `hi` etc.

Demo

修改

您可以尝试以下示例代码:(未经测试)

List<string> lst = new List<string>();
MatchCollection mcoll = Regex.Matches(sampleStr,@"(?<=[\(,])(.*?)(?=[\),])")

foreach(Match m in mcoll)
{
    lst.Add(m.ToString());
    Debug.Print(m.ToString());   // Optional, check in Output window.
}