programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Regular expression

Sudhir Pawar
Greenhorn
Posts: 25
• 1
I want to make regular expression to match any arithmetic expression which is in the form of

number of any digits
e.g 151
single operator from + - * /
number of any digits
e.g 151

e.g 151+151

RegExp = /([0-9])+([-|+|*])+([0-9])/;

Winston Gutkowski
Bartender
Posts: 10575
66
Sudhir Pawar wrote:I want to make regular expression to match any arithmetic expression which is in the form of...

It's always a good idea when working out regular expressions to:
(a) Work out what you want (which you've explained very well, which is why I gave you a +1).
(b) Work out what you DON'T want (which you haven't).

For example: You haven't explained:
1. Is a number without an operator correct?
2. Can there be spaces between the "numbers" and the operator?
3. Can there be more than one operator - ie: can an operator be followed by another operator?

I think if you answer those questions, you'll be closer to a solution, because you plainly have a grasp of the general syntax.

Winston

Sudhir Pawar
Greenhorn
Posts: 25
Questions
1. Is a number without an operator correct?
2. Can there be spaces between the "numbers" and the operator?
3. Can there be more than one operator - ie: can an operator be followed by another operator?

1. Number is always like following number means non negative no.
eg. 21

2. No space matching.

3. No only single operator has to be match from + - / * in between two numbers

Raymond Tong
Ranch Hand
Posts: 255
2
• 1
So, the followings are valid
a) 151
b) 151+151

which yours didn't handle the first case
yours actually formed by 3 parts, could you explain what they are.
By the time you explain to others and yourself, you should find the mistakes.

Sudhir Pawar
Greenhorn
Posts: 25
b)151+151
is valid one as per my expectations about regular expression

I want regular expression that can match integer_numberOperatorintegerNumber
eg. 456*45

Raymond Tong
Ranch Hand
Posts: 255
2
Sudhir Pawar wrote:b)151+151
is valid one as per my expectations about regular expression

I want regular expression that can match integer_numberOperatorintegerNumber
eg. 456*45

Could you explain the regular expression you have written part by part ?
I saw 3 pairs of brackets "(" and ")", I assume there are 3 parts, right ?

Campbell Ritchie
Marshal
Posts: 56529
172
Why have you got / at the end of it? Are you aware of the [] syntax?