tags:

views:

110

answers:

6

Hi everyone... this is maddeningly simple.... but i am still stumped whether my answer is right...

i want to match a string with following format....

  • 70-60
  • 50-40
  • 100-90 //(Edit: changed from 100 -90 to 100-90)
  • etc.....

i started learning regex just today.... and i worked out the pattern

/^[0-9]+-[0-9]+/$

to match the string . But testing it in some online regex testing tool i found that ti doesn't match...

any pointers that would help me..?

thank a lot in advance....

+4  A: 

the "$" needs to be within the regex

/^[0-9]+-[0-9]+$/

cgr
Still doesn't seems to work....
SpikETidE
I tested it to be safe. You will need to post your input and the output to show us what is going on. That regex site isn't working for me either :D Maybe its not your fault..hehe
cgr
+2  A: 

how about: \A[0-9]+\s*-\s*[0-9]+\Z

NOTES:

  • used \A and \Z instead of ^ and $
  • added \s* to account for possible spaces
Nestor
+1 for possible spaces (one of the examples has a space), but I don't see your point about ^ and $. What's wrong with those? BTW \A \Z don't work in some regex engines like JavaScript, while ^ and $ seem to be universal.
quosoo
Actually there is a space in one of the examples.
Kinopiko
The space was a mistake .. i'll edit it...i am using this regex with javascript.. thanks for the pointer....
SpikETidE
Well... technically ^ and $ are for beginning and end of line.. while \A and \Z stand for beginning and end of string (the string can have several lines)... You you choose what works for you.
Nestor
`^` and `$` mean beginning and end of the whole string by default. Only if the multiline option has been set do they mean beginning and end of line. (However, in a text editor's search function they may default to beginning and end of line because the other meaning is useless in that context.)
Alan Moore
A: 

You're a bit off on the regular expression syntax. What you want is:

/^[0-9]+\-[0-9]+$/
Matt
Don't need \-. 15 characters.
Kinopiko
the - between the numbers does not need to be escaped
Thomas Schreiner
I am testing with http://regexpal.com/ Both the above given regex seems not to work...where am i doing it wrong...????
SpikETidE
< 15 chars
beggs
If you're using a tester like regexpal, you should leave the slashes off. `^[0-9]+\-[0-9]+$`. Are you doing that?
Alan Moore
A: 

Using regex.powertoy.org

Regex (PHP/Perl-like syntax:

m/^[\d]+\s*-\s*[\d]+$/m

Test:

70-60
50-40
100 -90

matches all three test strings.

Note the trailing m makes this a multi-line match (leading m makes this a matching regex not a substituting regex). If you put all your test data in the test area and don't use a multi-line match it won't match (well it might match the first one...) because it's trying to match the whole input (^ to $) but that regex will only match each line. Change the test data to just one (70-60) and it works without the m.

beggs
A: 
^[\d]+-[\d]+$

Aswell as some of the other answers works with your test data IF, you check the ^$ matches line breaks.

/^[\d]+-[\d]+$/m

Would then be the expression you should use

Kristoffer S Hansen
A: 

Hi Everybody...

worked it out finally...!!!

  /[\d]+:?[\d]*/

this would check for each of the following

100

100:90

90:70

thanks everybody for u'r valuable inputs...

SpikETidE
It will also match `100:` - is that what you want? Usually, people want to match the separator only if there's something after it: `/\d+(?::\d+)?/`
Alan Moore