'LookAround or default regex if symbol is not present
I have got this regex
^\d+(?<=\d)_?(?=\d)\d*
My original goal is to match these patterns:
5555_555_5
But ignore
_55__
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_5is passed,555_555is also passed,_5is 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/
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
