Looking to use regex to extract specific text between two strings.
For example:
foo I have four toys bar //single line
foo //multiline
I
have
four
toys
bar
foo I have three pets bar
foo
I
have
three
pets bar
How can I extract the text between "foo" and "bar" that contains the word "four"?
Desired output:
I have four toys
I
have
four
toys
Here is my current regex code:
foo[\s\S]*?(four)[\s\S]*?bar
While this code works, it selects all text until the word "four" even if it's not within the "foo" and "bar" boundaries
foo I have three pets bar
foo I have four toys bar
foo
I
have
three
pets bar
My goal is to only extract text between "foo" and "bar" when it includes the word "four"