2

给定字符串

(“好”的比较)在健康或健身方面变得更好

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

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

我本可以完成上述操作,但是将字符串限制在倾斜单引号和单引号之间的正则表达式解决方案是什么

4

2 回答 2

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
于 2013-07-09T12:37:50.733 回答
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
于 2013-07-09T12:36:43.293 回答