I have two sentences as input. Let's say for example:
<span>I love my red car.</span>
<span>I love my car.</span>
Now I want to match every textpart inside the span-tags AND if available the color.
If I use the following regex:
/<span>(.*?)(?P<color>red)(.*?)<\/span>/ms
Only the line with the color is matched. So I thought let's use ?-operator (for one or zero).
/<span>(.*?)(?P<color>red)?(.*?)<\/span>/ms
Now both lines/sentences will be matched. Sadly the color isn't matched anymore.
The question is why? By using ".*?" before the color part, I thought I had made the regex non-greedy, so that the color part would match, if it's existent. But as told, it doesn't...
DOMDocument
– FleyI love my red car
string, which is just plain text. – Heidt