It should allow: 4- 3+ -2 1
It should not allow: 4-- 3+- %3 3%
Doing the following:^([-+]?(?:\d+))$
but it doesn't check correctly.
Here is a regex that works: ^[+-]?\d[+-]?$
. This will match one or more digit plus or minus on either side.
If you want to match numbers with a decimal point this one will work: "^[\+-]?\d+(\.\d+)?[\+-]?$
Edit* You do not need to use the escape character inside the character class. To preserve the information from my previous answer, the escape character of the +
is \+
and the link to the python regex documentation is here.
If you wan't to match the numbers separately you can use this regex:
^\d[\+|\-]$|^[\+|\-]\d$
Here You can see it in action link
If you want to match not all the single numbers and their signs but the whole row containing them, this regex will do the job:
^([-+]?\d[-+]? ?)+
Here you can play with it: https://regex101.com/r/2npw8e/2
If you just want to match each number this regex will do it:
^[-+]?\d[-+]?$
You maybe need to drop ^
and $
in the regex to match the numbers not only from the beginning of a line to the end, depending on how your actual data looks like.
-
or a+
only at the front of the word, you will need a group to capture those characters as many times as you would like