tags:

views:

40

answers:

2

I think the best way to ask this question is to provide an example.

I have a string:

string line = "12345";
 string pattern = "[0-9]{4}";
 MatchCollection collection = Regex.Matches(line, pattern);

This will return ONE match in the collection: "1234". BUT, is there a way to get it to return "1234" AND "2345"? So I want the regular expression to not skip characters that have been already been matched.

I'm very new to regular expressions so any help would be greatly appreciated. Thank you.

+1  A: 

Change the expression to:

 (?=\d{4})
Aliostad
This will return the correct number of matches, but not their content. Adding another set of parenthesis around the `\d{4}` ought to do it; you can then access the content via Groups[1] on each match.
KeithS
+1  A: 

"(?=(\d{4}))" will not only match both substrings, they'll tell you so; you can access the values of the matched substrings using Match.Groups[1] for each match.

KeithS