String的Python正则表达式以tr开头("以"结尾)

时间:2017-02-16 07:06:10

标签: python regex string

我想在Python中编写一个正则表达式,它将给我包含在tr("")中的所有字符串。

例如,

str = 'According to some, dreams express tr("profound aspects of personality")' and 'tr("Foulkes 184"), though others disagree.'

所以我想要字符串"个性的深刻方面"和" Foulkes 184"

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

不需要后视/前方,re.findall中的一个简单组就足够了:

> s = 'According to some, dreams express tr("profound aspects of personality") "and" tr("Foulkes 184"), though others disagree.'
> re.findall(r'tr\("(.*?)"\)', s)
['profound aspects of personality', 'Foulkes 184']

演示:https://regex101.com/r/xgG9AO/5

来自findall文档:

  

如果模式中存在一个或多个组,则返回组列表。