How to write a regular expression that match " 0-9()+-*/. "
+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
2009-10-27 16:02:16
+-* is an invalid range, put the '-' at the end and it should work
Inshallah
2009-10-27 16:04:01
I think the `-` should be the first character, like `[-0-9()+*/]`, no?
Victor
2009-10-27 16:06:29
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
2009-10-27 16:07:57
@inshallah thanks! forgot to escape the hyphen :D
Jass
2009-10-27 16:09:16
@Victor, putting the `-` first works as well :-), never knew.
Inshallah
2009-10-27 16:10:43
@victor putting the - first is a nice trick! +1 :)
Jass
2009-10-27 16:14:13
I dont think you need to escape (,),-,+ etc in a character class . It depends on the lanaguage doesnt it ?
Jass
2009-10-27 16:06:34
@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
2009-10-27 16:08:19
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
2009-10-27 16:12:31
+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
2009-10-27 16:32:56