tags:

views:

73

answers:

4

How to write a regular expression that match " 0-9()+-*/. "

+1  A: 
[0-9\(\)\+\-\*\./\"]
Lex
+3  A: 

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 *

Jass
+-* is an invalid range, put the '-' at the end and it should work
Inshallah
I think the `-` should be the first character, like `[-0-9()+*/]`, no?
Victor
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
@inshallah thanks! forgot to escape the hyphen :D
Jass
@Victor, putting the `-` first works as well :-), never knew.
Inshallah
@victor putting the - first is a nice trick! +1 :)
Jass
+4  A: 
[\d\(\)\+\-\*\/\.]
Chris Ballance
Hehe, all escaped +1.
Inshallah
I dont think you need to escape (,),-,+ etc in a character class . It depends on the lanaguage doesnt it ?
Jass
@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
The escape syntax depends on the language you're using.
Chris Ballance
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.. :)
Jass
+1  A: 

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