Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find each RegEx match in string

Tags:

id like to do something like

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

Thanks for any help...!

like image 897
kojoma Avatar asked Apr 02 '11 18:04

kojoma


People also ask

How do I find a character in a string in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


2 Answers

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.

like image 123
Oded Avatar answered Oct 10 '22 18:10

Oded


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);   } 
like image 35
Jason Avatar answered Oct 10 '22 19:10

Jason