I want to capture "foo" and any occurrence of "bar". Plus, I need to ignore any string between them and bar is optional.
Example text:
foo ignoreme barbarbar
foo ignoreme bar
foo ignoreme
foo something abcbar
Expected:
foo barbarbar
foo bar
foo
foo bar
A tried with this regex :
(foo)(?:.*)((?:bar)*)
But the .* capture all the end of the string:
foo
foo
foo
foo
So I changed it to lazy to stop the capture:
(foo)(?:.*?)((?:bar)*)
I almost got the same result, only foo is captured.
It seems it stop the capture to early, however, this almost works:
(foo)(?:.*?)((?:bar)+)
foo barbarbar
foo bar
<miss third line>
foo bar
But it misses the third line because the pattern "bar" must appear one time. Example here https://regex101.com/r/NIUPew/1
Any idea from a regex guru? Thanks!