I need a regular expression that will match
0
-9
, (
,)
,+
,-
,*
and /
.
10 Answers
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>)?

- 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
-
-
@ndnenkov what does "\g<1>" mean? I didn't find the relevant syntax? Can you explain it? – ink Feb 23 '22 at 02:10
-
-
@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
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]+)

- 11,337
- 9
- 44
- 72
-
1This 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
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 *

- 1,114
- 1
- 8
- 17
-
1
-
2
-
1Yes 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
-
[\d\(\)\+\-\*\/\.]

- 33,810
- 26
- 104
- 151
-
1I 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
-
1Yes, 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
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.

- 736
- 2
- 7
- 22
-
1I 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
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)']

- 29
- 3
-
2Although this code might solve the problem, a good answer should also explain what it does and how it helps. – Suraj Kumar Mar 26 '20 at 12:41
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.

- 686
- 9
- 19