开发者

Find each RegEx match in string

开发者 https://www.devze.com 2023-02-21 08:28 出处:网络
id like to do something like foreach (Match match in regex) { MessageBox.Show(match.ToString()); } Thank开发者_如何学Gos for any help...!There is a RegEx.Matches method:

id like to do something like

foreach (Match match in regex)
    {
    MessageBox.Show(match.ToString());
    }

Thank开发者_如何学Gos for any help...!


There is a RegEx.Matches method:

foreach (Match match in regex.Matches(myStringToMatch))
{
  MessageBox.Show(match.Value);
}

To get the matched substring, use the Match.Value property, as shown above.


from MSDN

  string pattern = @"\b\w+es\b";    
  Regex rgx = new Regex(pattern);    
  string sentence = "Who writes these notes?";

  foreach (Match match in rgx.Matches(sentence))
  {

     Console.WriteLine("Found '{0}' at position {1}", 
                       match.Value, match.Index);
  }


You first need to declare the string to be analyzed, and then the regex pattern. Finally in the loop you have to instance regex.Matches(stringvar)

string stringvar = "dgdfgdfgdf7hfdhfgh9fghf";
Regex regex = new Regex(@"\d+");

foreach (Match match in regex.Matches(stringvar))
{
    MessageBox.Show(match.Value.ToString());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消