
Untitled
By: a guest on
Jun 27th, 2012 | syntax:
None | size: 1.59 KB | hits: 9 | expires: Never
Regex issue with named captured pairs
start=2011-03-10T13:00:00Z;end=2011-03-30T13:00:00Z;scheme=W3C-DTF
#^start=(?P<publishDate>.+);end=(?P<expirationDate>.+);#ix'
#^(start=(?P<publishDate>.*?);)?(end=(?P<expirationDate>.*?);)?#xi
^(start=(?P<publishDate>.+);)?(end=(?P<expirationDate>.+));)?
/^(?= # from beginning, look ahead for start
.*b # any character 0 or more times (backtrack to match 'start')
start=(?P<publishDate>.*?); # put start date in publish
| (?P<publishDate>) # OR, put empty string publish
)
(?= # from beginning, look ahead for end
.*b # same criteria as above ...
end=(?P<expirationDate>.*?);
| (?P<expirationDate>)
)
/ix
<?php
$string = "start=2011-03-(start)10T13:00:00Z;end=2011-03-(end)30T13:00:00Z;scheme=W3C-DTF";
preg_match('/(?Ji)^
(?= (?| .* end = (?P<expirationDate> .*? ); | (?P<expirationDate>)) )
(?= (?| .* start = (?P<publishDate> .*? ); | (?P<publishDate>)) )
/x', $string, $matches);
echo "Published = ",$matches['publishDate'],"n";
echo "Expires = ",$matches['expirationDate'],"n";
print_r($matches);
?>
Published = 2011-03-(start)10T13:00:00Z
Expires = 2011-03-(end)30T13:00:00Z
Array
(
[0] =>
[expirationDate] => 2011-03-(end)30T13:00:00Z
[1] => 2011-03-(end)30T13:00:00Z
[publishDate] => 2011-03-(start)10T13:00:00Z
[2] => 2011-03-(start)10T13:00:00Z
)
#^start=(?P<publishDate>.*?);end=(?P<expirationDate>.*?);#ix'