Below is a pattern that matches numbers. It works almost. The second line should be matched with 99
but there is no match? Why?
(?<!\d[- ]|[\d.,])\(?-?(?:(?:[1-9]\d{0,2}(?:(?:[. ]\d{3})*|\d*))|0)(?:\b|[,]\d{1,3})-?\)?(?![\d.,\/]|-[\d\/])
100,00stk => 100,00
99stk => 99 \\ this is not matched
10,45stk => 10,45
https://regex101.com/r/nwRCKo/1
CodePudding user response:
The main problem here is the use of word boundary, but fixing the issue is not that evident.
The main point about the regex you have is that it matches some numbers in some specific context, and the lookarounds on both sides are meant to fail the match, so that you do not get a match at all. If you place a negative lookahead after an optional )
char, the regex engine may backtrack and you will still get this match. You need to prevent any backtracking here after removing the word boundary.
So, replace (?:\b|[,]\d{1,3})
with (?:[,]\d{1,3})?
and make all the subsequent optional patterns atomic by applying the possessive quantifiers:
(?<!\d[- ]|[\d.,])\(?-?(?:(?:[1-9]\d{0,2}(?:(?:[. ]\d{3})*|\d*))|0)(?:,\d{1,3})? -? \)? (?![\d.,\/]|-[\d\/])
See this regex demo.