• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expression  RSS feed

 
Sudhir Pawar
Greenhorn
Posts: 25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

I write folllowing but it wont work please help me to correct it.
RegExp = /([0-9])+([-|+|*])+([0-9])/;
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Answers
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
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got / at the end of it? Are you aware of the [] syntax?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!