'Getting overlapping regex matches in C#
I have the regex 1(0*)1
and the test string 1000010001
I want to have 2 matches, but I find that only 1 gets found :
var regex = new Regex("1(0*)1");
var values = regex.Matches(intBinaryString);
// values only has 1 match
regexonline seems to agree : https://regex101.com/r/3J9Qxj/1
What am I doing wrong?
Solution 1:[1]
You are already selecting the 1 in front of the second zero by the first match.
100001 0001
^^^^^^
This is the first match. The rest is just 0001
which does not match your regex.
You can circumvent this behavior if you are using lookaheads/lookbehinds:
(?<=1)(0*)(?=1)
Because you cannot use lookbehinds in JavaScript, it is enough to only use one lookahead, to prevent the overlapping:
1(0*)(?=1)
And a hint for your regex101
example: You did not add the global flag, which prevents more than one selection.
Solution 2:[2]
You need to match overlapping strings.
It means you should wrap your pattern with a capturing group ((
+ your pattern + )
) and put this consuming pattern into a positive lookahead, then match all occurrences and grab Group 1 value:
(?=(YOUR_REGEX_HERE))
Use
var regex = new Regex("(?=(10*1))");
var values = regex.Matches(intBinaryString)
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
See the regex demo
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |