Python re module: how to regex match extract a capture group from file
I have a file test.txt with this content:
CAR
one a. , z.
two b.
three c.
AIRPLANE
one a. , z.
two b.
three c.
BOAT
one a. , z.
two b.
I want to extract everything from CAR up to but not including AIRPLANE, and write that into output.txt. This regex gives me the everything I need in the capture group:
r"(CAR.*)AIRPLANE"s
. link: regex101.com/r/QJMJFh/1
To test my input test.txt is entering the program, I do this:
s = open('test.txt')
s_content = s.read()
print(s_content)
It succeeds and produces this:
CAR
one a. , z.
two b.
three c.
AIRPLANE
one a. , z.
two b.
three c.
BOAT
one a. , z.
two b.
However, when I run this:
s_output = re.search(r"(CAR.*)AIRPLANE"s, s_content).group(1)
print(s_output)
It fails and says
Cell In[85], line 4
s_output = re.search(r"(CAR.*)AIRPLANE"s, s_content).group(1)
^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
How else can I regex match extract a capture group from this file using re module?
Read more here: Source link