Home > Software design >  Regex with Named Capture Group
Regex with Named Capture Group

Time:10-29

I am trying to update a regex pattern to include a Named Capture Group. Currently, this regex pattern:

\b\d(?!(?:\d{0,3}([-\/\\.])\d{1,2}\1\d{1,4})\b(?!\S))(?:[^\n\d\$\.\%]*\d){14}\b

correctly returns 4 matches from this sample text:

AAA
43 42 040 012 036 00
43 42 090 037 124 00
53 07 010 005 124 00
06-14 301-830-081-49
BBB

When I revised the pattern to add a Named Capture Group it only returns 3 matches and misses the last one.

(?<myPattern>\b\d(?!(?:\d{0,3}([-\/\\.])\d{1,2}\1\d{1,4})\b(?!\S))(?:[^\n\d\$\.\%]*\d){14}\b)

How can I keep the Named Capture Group but still return 4 matches ?

See example here.

Thanks.

CodePudding user response:

Named capturing groups are still assigned numeric IDs. That is, (?<myPattern>[a-z] )(\d ) contains two groups, the first one - with ID 1 - is "myPattern" group matching one or more lowercase letters, and the second group is \d , with ID 2.

In your case, the problem arises due to the use of the \1 backreference later in the pattern. It refers to "myPattern" group value now, so the matching is incorrect.

To fix the issue, you need to replace \1 to the corresponding group, \2:

(?<myPattern>\b\d(?!(?:\d{0,3}([-\/\\.])\d{1,2}\2\d{1,4})\b(?!\S))(?:[^\n\d\$\.\%]*\d){14}\b)

See the regex demo.

  • Related