[英]Regular expression to return all characters between two strings
如何設計一個能捕獲2個字符串之間所有字符的正則表達式? 具體來說,從這個大字符串:
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 studies
和The second title
。
我想我將不得不使用re.findall(),並且我可以從這開始: re.findall(r'\\[([^]]*)\\]', big_string)
,這將給我所有的匹配方括號[ ]
,但我不知道如何擴展它。
>>> 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提取)。 這意味着當它找到...時會停止
\\]
,這是一個逃脫的]角色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.