1

I know there are many similar questions on SO about regex and matching, but I just cant get this to work!

I need to check if my string is in the specific format of:

13:30 - 14:00

2 numbers, colon, 2 numbers, space, dash, space, 2 numbers, colon, 2 numbers

This is my best effort so far...

$string = "13:30 - 14:00"; $regex = '^[0-9]{2}:[0-9]{2} - [0-9]{2}:[0-9]{2}$'; if (preg_match($regex, $string)) { echo "matched pattern"; } 

I am a regex noob, and I'm not sure why this isnt working.

Can someone help me to get this regex matching working?

7
  • Watch out that of 25:71 is not validated. Commented Sep 24, 2016 at 9:56
  • The strings will always be properly formatted as far as times are concerned, so I only need to check the format pattern, not validate the times. Commented Sep 24, 2016 at 10:34
  • OK then you can go with strlen($string) == 13 and $string[6] == '-' XD Commented Sep 24, 2016 at 10:37
  • No, because then "AB:cD - Ef:11" would pass, which doesnt fit my original question at all. @u_mulder answer was spot on and answers my question fully. Commented Sep 24, 2016 at 11:01
  • What I mean is that if the other string is guaranteed to be properly formatted don't do any validation, if it is not guaranteed then do all the validation, that's the spirit of design by contract. That way you don't repeat the same job that the other has done. Commented Sep 24, 2016 at 11:03

4 Answers 4

1

First: regexp should start/end with / (or #)

Second: - is a special symbol and should be escaped with \

$string = "13:30 - 14:00"; $regex = '/^[0-9]{2}:[0-9]{2} \- [0-9]{2}:[0-9]{2}$/'; if (preg_match($regex, $string)) { echo "matched pattern"; } 
Sign up to request clarification or add additional context in comments.

6 Comments

Bullseye! Thank you so much!
Downvote wasnt from me. This was the first and most useful answer, with explanation as to why my regex wasnt working.
@u_mulder I didn't downvote but still this can be improved because 13:61 will pass this regex.
My original question only asks to match the pattern. I dont need to validate the time, just pattern match. So this answer fully answers the question that I asked.
@Richard If you design by contract you don't need to validate the pattern at all, or very slightly.
|
1

If you don't want to accept 13:71 or 25:12 you need to extend the regex like this:

$string = "13:30 - 14:00"; $regex = '/^([0-1][0-9]|2[0-3]):[0-5][0-9] \- ([0-1][0-9]|2[0-3]):[0-5][0-9]$/'; if (preg_match($regex, $string)) { echo "matched pattern"; } 

Comments

1
 $string = "13:30 - 14:00"; $regex = '/^([0-1][0-9]:[0-5][0-9])|(2[0-3]:[0-5][0-9])|(24:00) \- ([0-1][0-9]:[0-5][0-9])|(2[0-3]:[0-5][0-9])|(24:00)$/'; if(preg_match($regex, $string)){ echo "matched pattern"; } 

As it seems like time, and for that it must be checked ! 13:70 is not a valid time i guess

5 Comments

- is a special symbol in regexps.
Thanks, I overlooked it!
@DominiqueLorre is that ok now ?
Ye we have similar answers now.
@u_mulder: no, it's false.
-1

You may try it like this:

<?php $strTime = "13:30 - 14:00"; $time = '(\d{2})(:)(\d{2})'; $rx = '#^' . $time . '( \- )' . $time . '$#'; if (preg_match($rx, $strTime)) { echo "Pattern was found..."; // DO WHATEVER YOU WANT TO DO WHEN PATTERN IS MATCHED. } 

1 Comment

Your answer isn't wrong but doesn't explain what was the initial problem. There's no need to put capture groups everywhere.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.