如何用正则表达式来区分“单词”?蟒蛇

时间:2013-07-09 12:33:32

标签: python regex string

给出字符串

  

(“好”的比较)在健康或健身方面有所改善

我需要提取倾斜的单引号和单引号之间的第一个字符串

line = "(comparative of `good') changed for the better in health or fitness"
line.split("`")[1].split("'")[0]

我本可以做到以上但是什么是正则表达式解决方案,以获得倾斜的单引号和单引号之间的字符串

2 个答案:

答案 0 :(得分:5)

import re
line = "(comparative of `good') changed for the better in health or fitness"
match = re.search(r"`(.*?)'", line)
print(match.group(1))

产量

good

答案 1 :(得分:1)

这样的东西?

import re

your_line = "(comparative of `good') changed for the better in health or fitness"
word = re.search(r"`([^']*)'", your_line).group(1)  # good