Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi All,

i want to write a regular express for text box in asp.net using RegularExpressionValidator control.

user can type formula like and also i have achieved few ways using below link
http://public.kvalley.com/regex/regex.asp[^]
and regular expression "^([0-9]+[^-\+\?\*\.\|\{\}\\\[\]\(\)]$)|(\[\w*[^0-9]\][/-\+\?\*\.\|\{\}\\\[\]\(\)][0-9]+$)|([0-9]*[-/\+\?\*\.\|\{\}\\\[\]\(\)]+\[\w*[^0-9]\])$"

1)[vijayr]+ 2
2)2+[vijayr]
3)only number

but now i need to write a formula with complex case like

1) (([vijayr]+10) * [vijayr3]+100)/12 *([vijayr])
2) 10 /([vijayr]+100)-10



please give me any clue on this

Thanks
Vijayr
Posted

Looks like what you need to design is a Recursive Descent Parser[^].

You'll need more than a big regular expression, if you want to keep your sanity :)
 
Share this answer
 
Comments
vijayr 8-Sep-10 10:33am    
hi,
can u explain me bit or some kind of logic
Posting this as another answer, so I can format it a bit better... Really a reply to your comment above...

The wikipedia article I linked explains the concept pretty thoroughly. You won't be able to do this with a single regex. I would suggest building validation into your parser, since I assume you'll want to actually evaluate these somehow...

If you really want to use regexes just to validate, you could maybe run through from the bottom up... Use one regex or a series of them to process simple expressions:
* X + Y
* X - Y
* X / Y
* X * Y
* (X)

(Where either argument can be a number or a word in brackets)

Then apply each of the expressions to your complex formula. If you get a match, replace it with "[X]" and continue to loop. Keep trying to apply them all until you either reduce the entire formula to one variable (Success), or can no longer get a match (Failure).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900