I have the following line:
I _ foo and _
I want to match anything (case insensitive) that matches the text and has a words that replace the underscores e. g.
JavaScript
x
4
1
I want foo and bar
2
I implemented foo and foo
3
i use Foo and bar
4
So in this case I want to get the whole sentences back: ["I want foo and bar", "I implemented foo and foo", "i use Foo and bar"]
To be honest, I don’t have any idea.
Advertisement
Answer
JavaScript
1
13
13
1
import re
2
3
strings = [
4
"I want foo and bar",
5
"I implemented foo and foo",
6
"i use Foo and bar"
7
]
8
9
for string in strings:
10
fw, sw = re.search(r"i (.+?) foo and (.+?)(?:s|$)", string, flags=re.I).groups()
11
print("first word:", fw)
12
print("second word:", sw)
13
Where flags=re.I
is needed for case insensitivity.