Chris
Chris

Reputation: 953

Regex works as grep, not with preg_match

I've compiled a regex (based on other posts) that matches multiple date formats perfectly when testing with grep in Text Wrangler, but when I put it into PHP and use it with preg_match it doesn't match anything.

Here is the regex:

/^(((?:0?[1-9]|1[012])|(?:0?[1-9]|[12][0-9]|3[01])|([a-zA-Z]+))([.]?[-.\\/\s]))?(((?:0?[1-9]|1[012])|(?:0?[1-9]|[12][0-9]|3[01]))([,]?[-.\\/\s]))?((?:20|19)[0-9]{2})$/

It should match (and does in Text Wrangler):

03/12/2000
4 4 2011
16 04 1985
11/11/1911
04.13.2013
May 12, 1912
July 13 2012
March 1999
2015
1944

It doesn't match:

2000 12 4
11/11/1111
40.13.2013

Is there something I'm missing that should be escaped or formatted differently for preg_match vs. grep

Upvotes: 0

Views: 656

Answers (2)

Sunny
Sunny

Reputation: 119

it is working on regex testing tool.

try it at

http://toolspot.org/regex_tester.php

Upvotes: 0

DhruvPathak
DhruvPathak

Reputation: 43265

You need to use "s" flag to make it 'dotall' match ( to include newlines)

preg_match('#THE_REGEX_PATTERN#is',$myStr,$matches);

Upvotes: 1

Related Questions