clarkk clarkk - 4 months ago 19
PHP Question

regexp match pattern and prefix

I need a pattern to match a date and I need the prefix before the date

$prefix = '^(.*)';

preg_match("/$prefix(\d+) (may) (2016)/mi", $input, $matches)


input 1 (working)



3 may 2016
\\1
\\2 3
\\3 may
\\4 2016


input 2 (not working)



17 may 2016
\\1 1
\\2 7
\\3 may
\\4 2016


input 3 (not working)



prefix 17 may 2016
\\1 prefix 1
\\2 7
\\3 may
\\4 2016

Answer

Try this, I think, it can work:

$prefix = '^(.*?)';

preg_match("/$prefix(\d{1,2}) (\w{3,10}) (2016)/", $input, $matches);

Here you can test it.