开发者

C# Regular Expression -- can a character be matched more than once in a single 'Match' call?

开发者 https://www.devze.com 2023-01-16 13:27 出处:网络
I think the best way to ask this question is to provide an example. I have a string: string line = \"12345\";

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 h开发者_Go百科elp would be greatly appreciated. Thank you.


"(?=(\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.


Change the expression to:

 (?=\d{4})
0

精彩评论

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

关注公众号