Regular expression to return all characters between two lines

How can I create a regex that will capture all characters between two lines? In particular, from this big line:

Studies have shown that...[^title=Fish consumption and incidence of stroke: a meta-analysis of cohort studies]... Another experiment demonstrated that... [^title=The second title]

I want to extract all the characters between [^title=and ], that is, Fish consumption and incidence of stroke: a meta-analysis of cohort studiesand The second title.

I think I will have to use re.findall (), and I can start with this: re.findall(r'\[([^]]*)\]', big_string)which will give me all the matches between the square brackets [ ], but I'm not sure how to expand it.

+3
source share
1 answer
>>> text = "Studies have shown that...[^title=Fish consumption and incidence of stroke: a meta-analysis of cohort studies]... Another experiment demonstrated that... [^title=The second title]"
>>> re.findall(r"\[\^title=(.*?)\]", text)
['Fish consumption and incidence of stroke: a meta-analysis of cohort studies', 'The second title']

Here is a regex breakdown:

\[ is an escaped character.

\^ is an escaped character.

title= matches title =

(.*?) , , ( findall ). , , ...

\], .

+5

All Articles