The main problem with the original regular expression is that [ escaped, and so the literal [ matched, and another problem is related to (\d+)[^x] , which captures 1 + digits and is written to group 1 and then a [^x] matches any char, but x . This means that it can also match a digit (as in your case with -90x , [+-] matches - , (\d+) matches and captures 9 and [^x] matches 0 ).
A more suitable regular expression is to include the \d pattern with x in the negative view:
[+-](\d+)(?![\dx])
See the demo of regex .
Template Details :
[+-] - either + or -(\d+) - Capture group 1 corresponding to 1 or more digits(?![\dx]) - a negative result that does not match if the numbers 1 + follow the number or x .
source share