I'm new to regex and having some difficulty. Here is some sample code:
<?php
$t = "Apple: blah blah blah 12 Mar 2003 http";
$pat = '/(Apple|Orange): (?P<extract>.+)(\d{1,2} \w{3} \d{4})? http/';
preg_match($pat, $t, $matches);
print trim($matches["extract"]);
?>
I have 2 questions:
1) I want it such that the day in the date can be either 1 or 2 digits, so I would like it to return "blah blah blah" instead of "blah blah blah 1".
2) I would also like the date to be optional (it may be there sometimes, not other times), but can't get it to work by adding a ? after the date grouping:
$pat = '/(Apple|Orange:): (?P<extract>.+)(\d{1,2} \w{3} \d{4})? http/';
In this case it returns: "blah blah blah 12 Mar 2003"
This seems simple but I can't figure it out. Help would be greatly appreciated!