I have a string from which I need to capture one and possibly two substrings (using PHP):
I am unable to make my pattern capture the second pattern unless I make it mandatory in the pattern string. This makes the pattern fail when only the first pattern is available in the subject.
I'm stumped. This shouldn't be that hard.
<?php
// sometimes the subject looks like this:
//$subject = 'pattern 111 -then some random junk-';
$subject = 'pattern 111 -then some random junk- pattern 222';
preg_match('/(pattern 111)(.*?)(pattern 222)?/', $subject, $matches);
print_r($matches);
?>
This is what I get from the above:
Array
(
[0] => pattern 111
[1] => pattern 111
[2] =>
)
Seems to boil down to how do I make {0,1} (that is, the final ? operator in the pattern) be more greedy (ironic given that as a quantifier modifier it does the opposite)
Try this here
(pattern 111)(?:.*(pattern 222))?
See it here on Regexr
I made the second group a non capturing one, so you do have two capturing groups, the first one contains the mandatory part and the second one would contain the optional part.
It will not work to combine the lazy quantifier with an optional part at the end, but if the optional part is missing, there is no need to match the part following your mandatory pattern, so just include the unknown junk into the optional part.