Why doesn't this regex match the second 6 digit number?
1 answer
The regex "(^|\D)(\d{6})(\D|$)"
matches and consumes a non-char before and after 6 digits (i.e. the space after is 123456
consumed during the first iteration).
Use indecent construction, lookbehind and lookahead:
"(?<!\d)\d{6}(?!\d)"
See .NET regex demo .
A negative lookbehind (?<!\d)
does not match if the near is to the left of the current location, and a (?!\d)
negative lookahead does not match if there is a digit immediately after the current position, without actually moving the regex index within the string.
+6
source to share