Home > Back-end >  Is there a way to get the regex that I am using to match 4 numbers only?
Is there a way to get the regex that I am using to match 4 numbers only?

Time:11-18

The issue I am having is that the Regex I am using is matching more that the different variations of 8801. I need the regex to match 8801 only and not 01 and 108. The link below shows how the regex I am using is matching more than the desired 8801. Any help would be greatly appreciated.

"\b[8]*(?:1[8]*[8]?0|0[8]*[8]?1)[8]*\b"

See my regex demo.

CodePudding user response:

You can use

\b(?=\d{4}\b)8*(?:18*0|08*1)8*\b

See the regex demo. Note that [8]*[8]? = 8* (zero or more 8 chars) and [8] = 8 (the square brackets are redundant in this case).

The (?=\d{4}\b) positive lookahead requires four digits followed with a word boundary position immediately to the right of the current position.

  • Related