Home > OS >  LookAround or default regex if symbol is not present
LookAround or default regex if symbol is not present

Time:05-08

I have got this regex

^\d (?<=\d)_?(?=\d)\d*

My original goal is to match these patterns:

  • 5
  • 55
  • 5_5
  • 55_5

But ignore

  • _5
  • 5_
  • _

As long as I understand, it matches at least 1 digit from the beginning of the line and anderscore if it is surrounded by digits. Pretty simple. So,

  • 5_5 is passed,
  • 555_555 is also passed,
  • _5 is not passed, it is expected,
  • _ also not passed.

In additon, 55 is also passed, which is fine.

But for some reason 5 is not passed as well. Why? It is single digit and it has to passed even though there is no underscore sign later. Any ideas why is this happening? Thanks.

Tested on https://regex101.com/

CodePudding user response:

The reason is because the pattern should match at least 2 digits.

This is due to the ^\d and asserting another digit to the right (?=\d)

In your pattern, you can remove the lookaround assertions, as you are also matching the digits that you are asserting so they are redundant.

Your pattern can be written as ^\d _?\d where you can see that you have to match at least 2 digits with an optional underscore.


To get the current matches that you want, you might write the pattern as:

^\d (?:_\d )?$

Explanation

  • ^ Start of string
  • \d Match 1 digits
  • (?:_\d )? Optionally match _ and 1 digits (to prevent an underscore at the end)
  • $ End of the string

Regex demo

  • Related