12

I need a regular expression that will match 0-9, (,),+,-,* and /.

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
DNB5brims
  • 29,344
  • 50
  • 131
  • 195

10 Answers10

21

The accepted answer can't handle a lot of basic cases. This should do the job:

^([-+]? ?(\d+|\(\g<1>\))( ?[-+*\/] ?\g<1>)?)$

Explaination:

We want to match the entire string:

^...$

Expressions can have a sign:

[-+]? ?

An expression consists of multiple digits or another valid expression, surrounded by brackets:

(\d+|\(\g<1>\))

A valid expression can be followed by an operation and another valid expression and is still a valid expression:

( ?[-+*\/] ?\g<1>)?
ndnenkov
  • 35,425
  • 9
  • 72
  • 104
  • how use \g<1> in javascript? I understand that this is only with PCRE regex? https://regex101.com/r/HdBU3E/2/ – hizmarck Jan 29 '20 at 01:57
  • @hizmarck indeed, this works only for PCRE regex engines. – ndnenkov Jan 29 '20 at 06:57
  • @ndnenkov what does "\g<1>" mean? I didn't find the relevant syntax? Can you explain it? – ink Feb 23 '22 at 02:10
  • @hizmarck hi, have you solved it? I also want to use it in JavaScript – ink Feb 23 '22 at 02:11
  • @ink it's [regex group recursion](https://www.regular-expressions.info/subroutine.html). It's only available in PCRE engines though (the JS one isn't). – ndnenkov Feb 23 '22 at 06:22
  • @hizmarck, Too bad, do you have any good suggestions for match math expressions in js? Is your problem finally solved? – ink Feb 23 '22 at 07:47
  • 1
    @ink it's impossible to match infinitely balanced brackets with non-recursive regexes. You can make some compromises based on your specific case. For example, if you don't allow for `(`/`)` this is perfectly solvable. Or you can have a fixed number of nestings (say a person can't nest brackets inside brackets more than 3 times). Alternatively you can allow for infinite nesting and use the regex just to make sure you have equal number of opening and closing brackets and afterwards add some code that checks the correct balancing if the regex matched. – ndnenkov Feb 23 '22 at 08:21
  • @ndnenkov Thanks, looks like I have to limit the number of parentheses the user can enter – ink Feb 23 '22 at 09:41
15

It looks like you might be trying to match numeric expressions like 5+7-3.

This should match them :

([-+]?[0-9]*\.?[0-9]+[\/\+\-\*])+([-+]?[0-9]*\.?[0-9]+)
Mongus Pong
  • 11,337
  • 9
  • 44
  • 72
  • 1
    This answer does not incorporate the brackets mentioned in the question. What if you have an operation (2+3)*(5/(7-1))? – Kunal Kapoor Sep 02 '15 at 17:49
  • In addition, that incorrectly says `25*(53+5` is valid and `2*(3)` is not. If you wanna try and find more: https://jsfiddle.net/a7eg9a78/ – abluejelly Dec 07 '15 at 23:26
8

I think you are looking for character classes

[0-9()+\-*/.]

This should match a word that contains any number from 0 to 9 or ( ,),+,- ,/ or *

Jaskirat
  • 1,114
  • 1
  • 8
  • 17
  • 1
    +-* is an invalid range, put the '-' at the end and it should work – Inshallah Oct 27 '09 at 16:04
  • 2
    I think the `-` should be the first character, like `[-0-9()+*/]`, no? – Victor Oct 27 '09 at 16:06
  • 1
    Yes I think the solution is character classes, but in response to inshallah, shouldn't just escaping the '-' be sufficient? My understanding is that depending on the system (which has atm not been specified by OP), characters such as that can have special meanings, in fact so can *, and the brackets. I think OP would do best to read whatever documentation there is for his particular regex system. – nullpointer Oct 27 '09 at 16:07
  • @Victor, putting the `-` first works as well :-), never knew. – Inshallah Oct 27 '09 at 16:10
7
[\d\(\)\+\-\*\/\.]
Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
  • 1
    I dont think you need to escape (,),-,+ etc in a character class . It depends on the lanaguage doesnt it ? – Jaskirat Oct 27 '09 at 16:06
  • @Jass, in Perl at least you are right. You do need to take care however that you either escape `/` or write the regexp like m~[\d()+*/.-]~. – Inshallah Oct 27 '09 at 16:08
  • 1
    The escape syntax depends on the language you're using. – Chris Ballance Oct 27 '09 at 16:10
  • 1
    Yes, we escape the / there because the language requires it so that it can identify the / and pass it on to the regex engine instead of eating it. Like you say the usage of m~~ is a workaround.. :) – Jaskirat Oct 27 '09 at 16:12
3

If you need a regex to match an arithmetic expression like this: 3+2-24*2/2-1 you can try this:

String reg1="([0-9]+[\\+\\-\\*\\/]{1}[0-9]+)+([\\+\\-\\*\\/]{1}[0-9]+)*";

You can add the bracket where do you want if you'll edit this regex.

Antonio1996
  • 736
  • 2
  • 7
  • 22
  • 1
    I marked you answer as useful because it helped me in my work. All above answer did not help me much. Thanks. – Saad Anees Sep 07 '17 at 05:13
2
regex  = '(?:[0-9 ()]+[*+/-])+[0-9 ()]+'
string = 'mig 2*7 5+ 43 asf 4 3+32 *33 as 0 fa 3 5+9 m (3 + 5) - 9*(99)'
re.findall(regex, string)
# answer
# [' 2*7 5+ 43 ', ' 4 3+32 *33 ', ' 3 5+9 ', ' (3 + 5) - 9*(99)'] 
Nafe Kzir
  • 29
  • 3
1
[0-9\(\)\+\-\*\./\"]
user254875486
  • 11,190
  • 7
  • 36
  • 65
1

This regex helps me, just take a note here maybe it helps others.

^[0-9+\-*\/\(\)]*$
Gfew
  • 41
  • 1
  • 7
1

This works for mathematical expression with 4 decimal places:

^([-+]? ?(\d+(\.\d{0,4})?|\(\g<1>\))( ?[-+*\/] ?\g<1>)?)$

It's inspired in base of @ndnenkov answer.

I hope that can be useful for someone.

hizmarck
  • 686
  • 9
  • 19
0
^[0-9()+\-*.\/]*$

https://regex101.com/r/377yXA/1

This regex worked for me

Morez
  • 2,048
  • 5
  • 36
  • 49