-2

Stumped by this one O_o...

if(preg_match("(\d{1,2})\:(\d{2})", "5:00", $matches) == 1) echo "works";
else echo "don't work";

Will echo

don't work

But the same regex on Regex 101 seems to work fine: http://regex101.com/r/mW1dN9

Do I need a global flag or something?

parker.sikand
  • 1,371
  • 2
  • 15
  • 32

2 Answers2

3

Your regex pattern needs some delimiters.

if(preg_match("#(\d{1,2})\:(\d{2})#", "5:00", $matches) == 1) echo "works";
else echo "don't work";
Peter O'Callaghan
  • 6,181
  • 3
  • 26
  • 27
  • cool that worked. Why exactly are the delimiters necessary? – parker.sikand Mar 06 '13 at 17:13
  • If you need to use any flags (e.g., global, case-insensitive, etc.), the delimiters show the regexp engine where to find them in the string. They have to be there, even if you're not using flags. – Xophmeister Mar 06 '13 at 17:15
  • They separate the pattern from the flags. If you were to place an i after the closing delimter, it would make the pattern case insensitive. http://php.net/manual/en/regexp.reference.delimiters.php – Peter O'Callaghan Mar 06 '13 at 17:16
  • so it sounds like a case of "that's just how it works"? – parker.sikand Mar 06 '13 at 17:17
2

You need to put your regular expression within delimiters:

if(preg_match("/(\d{1,2}):(\d{2})/", "5:00", $matches) == 1) echo "works";
else echo "don't work";

Also, you don't need to escape the :, but it works either way.

Xophmeister
  • 8,884
  • 4
  • 44
  • 87