我需要一种方法来查找字符串的所有实例,然后获取从该字符串末尾到另一个字符串的所有内容

I need a way to find all instances of a string and then get everything from the end of that string to another string

我需要一种方法来查找字符串的所有实例,然后获取从该字符串末尾到另一个字符串的所有内容。

我想要的例子:

string = '''
<country:England> abcdafsasffasfafasasasffsafdafuiugiugigoog
af <country:Spain> asfasfigfasgyafsguiosfbsafbuiasfuis
faiufsabasfbuiasfbfas <country: Italy>asfasfhasfhgasfgasiafs'''

对于国家这个词的所有实例,我想得到

之后的国家名称
Output = [England, Spain, Italy]

正则表达式 findall 可以为您做到这一点。以下将捕获所有单词字符组 (\w+),其模式以字符串“country:”开头,后跟可选的 space,并以“>”

结尾
import re

output=re.findall(r"country: ?(\w+)>",string)

print(output) 结果:

['England', 'Spain', 'Italy']