My goal is to find all matches to some pattern in text.
Let’s say my pattern is:
h.*o
This means I am searching for any text starting with 'h' ending with 'o' and having any number of chars in between (also zero).
My understanding was that method Matches() would deliver multiple matches according description (see MSDN).
const string input = "hello hllo helo";
Regex regex = new Regex("h.*o");
var result = regex.Matches(input);
foreach (Match match in result)
{
Console.WriteLine(match.Value);
}
My expectation was:
1. "hello"
2. "hllo"
3. "helo"
4. "hello hllo"
5. "hello hllo helo"
To my surprise returned matches contain only one string – the whole input string.
"hello hllo helo"
Questions:
- Which one is wrong: my expectation, my regex or usage of class?
- How to achieve the result as shown in my example?
Thanks in advance.
The
*is greedy – it will try matching as many characters as it possibly could. You can make it reluctant by following it by question mark, but a better solution is to excludeofrom the list if characters the.matches, like this:Here is a link to very good explanation of greedy vs. reluctant.