Search code examples
regexregex-group

How can I modify my regex so that it includes 1171


https://regex101.com/r/QTdaAT/1

My current regex matches all numbers that have 117 except for 1171. I am trying to modify the regex so that it includes 1171 1711 7111. I included a link that provides examples of the matches that are made and missed with the regex I am using. Any help will be greatly appreciated.

"\b(?=[02-9]1[02-9]1[02-9]\b)(?=\d{3})\d7\d*\b"

example:

matches 1172, 1173 1174

Needs to include 1171.


Solution

  • To match all numbers that contain at least two 1 and one 7

    Then this simplified regex pattern will match them

    \b(?=\d*1\d*1)(?=\d*7)\d+\b
    

    The first lookahead (?=\d*1\d*1) checks for two 1 digits.
    The second lookahead (?=\d*7) checks for a 7 digit.