0

I am stuck on making a regular expression that matches numbers from 01 to 56, with a space between them but not a space a the end. Strings samples look like these ones:

"33 44 51 52 53 54"
"01 05 09 14 25 36"

To achieve that, i am using the following expression (and testing it at http://regexpal.com/):

([0-5][0-9] +){6}

It works fine, but the problem is that it only matches those strings only if there is a space at the end:

"01 05 09 14 25 36 "

How do i fix this?

How do i limit it to match until 56 and not 59? and to start from 01 and not from 00?

Alan Moore
  • 68,531
  • 11
  • 88
  • 149
Pathros
  • 7,107
  • 11
  • 67
  • 107
  • 1
    [This is an answer](http://stackoverflow.com/a/22131040/2736496) I wrote on using regular expressions to match a numeric range. Also, please consider bookmarking the [Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496) for future reference. – aliteralmind Apr 20 '14 at 18:06
  • Are you really using the NSRegularExpression class from Cocoa/iOS? If not, what flavor/language/tool are you using? – Alan Moore Apr 21 '14 at 01:12
  • @AlanMoore , i am using PHP for this ;-) Why? – Pathros Apr 22 '14 at 01:45
  • [tag:nsregularexpression] applies to one particular regex flavor, and should only be used if you're working with that flavor. [tag:regex] is the only tag you need for regexes in general. You should always use it along with a tag for the flavor ([tag:php] in this case), because no two flavors are exactly alike. In case you're wondering, we use the name "regex" instead of "regular-expression" to emphasize that we're **not** talking about theory-pure, computer-science regular expressions. At least, that's why *I* do it. :D – Alan Moore Apr 22 '14 at 04:59
  • All right, @AlanMoore. I have added the PHP tag ;) – Pathros Apr 23 '14 at 02:23

3 Answers3

4

Try this:

^((0[1-9]|[1-4][0-9]|5[0-6]) ){5}(0[1-9]|[1-4][0-9]|5[0-6])$

This will match any number from 01 to 56, where each number is followed either by a space, repeated 5 times, followed by another number from 01 to 56. The start (^) and end ($) anchors ensure that no other characters are allowed before or after the matched string.

However, depending on your language / platform of choice, for something like this, it's probably better to simply split the string by spaces and parse each substring as an integer, and validate that the number is in the appropriate range.

p.s.w.g
  • 136,020
  • 27
  • 262
  • 299
1

It's easier than you think. But this still matches everything from 00 to 59. So I have not answered the 2nd question.

([0-5][0-9] +){5}[0-5][0-9]
SzG
  • 11,409
  • 4
  • 23
  • 37
0

Separate matches for numbers starting with 5 and 0:

^((0[1-9]|[1-4][0-9]|5[0-6])( +|$)){6}(?<! )$

(?<! ) a negative look behind to check the last space

Explanations:

^       # Start of string
    (   # 1st optional capturing group
        (   #2nd optional capturing group
            0[1-9]      # Match two-digit numbers start with 0 and not 00
            |           # Or
            [0-4][0-9]  # Match two-digit numbers start with 0 through 4
            |           # Or
            5[0-6]      # Match 50 through 56
        )               # 0 or more spaces
            ( +|$)       # 1 or more spaces after digits but not the last
    ){6}    # For 6 times
    (?<! )  # No space at the end
$       # End of string

Live demo

Notice that the first line has an space at the end

revo
  • 43,830
  • 14
  • 67
  • 109