0

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.

2
  • your regex will only allow a - 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
    – Ali
    Dec 9, 2016 at 21:55
  • do you want to find matches within a sequence of digits or to match one by one separately? Dec 9, 2016 at 22:05

3 Answers 3

3

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.

0
2

If you wan't to match the numbers separately you can use this regex:

^\d[\+|\-]$|^[\+|\-]\d$

Here You can see it in action link

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.