How to prevent regex from stopping at the first match of alternatives ?

Posted by miket2e on Stack Overflow See other posts from Stack Overflow or by miket2e
Published on 2010-05-05T13:32:13Z Indexed on 2010/05/05 14:28 UTC
Read the original article Hit count: 207

Filed under:
|
|

If I have the string hello world , how can I modify the regex world|wo|w so that it will match all of "world", "wo" and "w" rather than just the single first match of "world" that it comes to ?

If this is not possible directly, is there a good workaround ? I'm using C# if it makes a difference:

Regex testRegex = new Regex("world|wo|w");
MatchCollection theMatches = testRegex.Matches("hello world");
foreach (Match thisMatch in theMatches)
{
   ...
}

© Stack Overflow or respective owner

Related posts about regex

Related posts about c#