两个字符串之间的所有字符正则表达式

3
我该如何设计一个正则表达式来捕获两个字符串之间的所有字符? 具体来说,从这个大字符串开始:
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]
我想提取在[^title=]之间的所有字符,即Fish consumption and incidence of stroke: a meta-analysis of cohort studiesThe second title
我认为我需要使用re.findall()函数,并且可以从以下代码开始: re.findall(r'\[([^]]*)\]', big_string),这将给我所有方括号[ ]之间的匹配项,但我不确定如何扩展它。
1个回答

5
>>> 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']

以下是关于正则表达式的详细解释:

这是一个正则表达式的分解:

\[ 是一个转义的 [ 字符。

\^ 是一个转义的 ^ 字符。

title= 匹配 title=。

(.*?) 匹配任何字符,非贪婪地,并将它们放入一个组中(用于提取 findall 结果)。这意味着它会在找到...时停止。

\] 是一个转义的 ] 字符。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接