I need to capture the optional texts in a pattern, with the extracted examples i, 9, Caffè and you in the following matched scenarios:
Valid matches:
love!
9 love!
love Caffè!
i love you!
Invalid matches:
love
iloveyou
My rule: [*] love [*]!, with [*] meaning blank text can be accepted to be captured.
I tried to make the regex ((?:.+) )?love( (?:.+))?! (online) but it failed to capture the correct texts.
You may swap your capturing with non-capturing groups:
(?:(.+) )?love(?: (.+))?!
^^ ^^
See the regex demo
Now, the text that you will have inside groups will be without spaces as they are part of the non-capturing groups that are only meant to group subpatterns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With