Take the 2-minute tour ×

This is maddeningly simple, but I am still stumped as to whether my answer is right.

I want to match a string with following format:

  • 70-60
  • 50-40
  • 100-90
  • etc.....

I just started learning about regexes today, and I worked out the following pattern to match the string:


But upon testing it in an online regex testing tool, I found that it doesn't match.

Any pointers that would help me? Thanks a lot in advance.

share|improve this question
is the text you want to match the only text, or is it inside some other string - that has consequences for your regex. –  Hans Kesting Nov 6 '09 at 7:34

6 Answers 6

the $ needs to be within the regex

share|improve this answer
Still doesn't seems to work.... –  SpikETidE Nov 6 '09 at 7:12
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 Nov 6 '09 at 7:35

how about:



  • used \A and \Z instead of ^ and $
  • added \s* to account for possible spaces
share|improve this answer
+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 Nov 6 '09 at 7:08
Actually there is a space in one of the examples. –  user181548 Nov 6 '09 at 7:09
The space was a mistake .. i'll edit it... i am using this regex with javascript.. thanks for the pointer.... –  SpikETidE Nov 6 '09 at 7:20
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 Nov 6 '09 at 7:21
^ 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 Nov 6 '09 at 13:29

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

share|improve this answer
Don't need \-. 15 characters. –  user181548 Nov 6 '09 at 7:10
the - between the numbers does not need to be escaped –  Thomas Nov 6 '09 at 7:12
I am testing with regexpal.com Both the above given regex seems not to work... where am i doing it wrong...???? –  SpikETidE Nov 6 '09 at 7:15
< 15⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪⁪ chars –  beggs Nov 6 '09 at 7:20
If you're using a tester like regexpal, you should leave the slashes off. ^[0-9]+\-[0-9]+$. Are you doing that? –  Alan Moore Nov 6 '09 at 13:35

Using regex.powertoy.org

Regex (PHP/Perl-like syntax:



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.

share|improve this answer

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


Would then be the expression you should use

share|improve this answer
up vote 0 down vote accepted

worked it out finally...!!!


this would check for each of the following




thanks everybody for u'r valuable inputs...

share|improve this answer
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 Nov 6 '09 at 13:41

Your Answer


