2
(比较'好'的),改变在健康越好
我需要提取倾斜的单引号和单引号之间界定的第一串
line = "(comparative of `good') changed for the better in health or fitness"
line.split("`")[1].split("'")[0]
我可以做以上,但什么是正则表达式的解决方案,以获得倾斜的单引号和单引号之间界限的字符串
(比较'好'的),改变在健康越好
我需要提取倾斜的单引号和单引号之间界定的第一串
line = "(comparative of `good') changed for the better in health or fitness"
line.split("`")[1].split("'")[0]
我可以做以上,但什么是正则表达式的解决方案,以获得倾斜的单引号和单引号之间界限的字符串
import re
line = "(comparative of `good') changed for the better in health or fitness"
match = re.search(r"`(.*?)'", line)
print(match.group(1))
产生
good
像这样的东西?
import re
your_line = "(comparative of `good') changed for the better in health or fitness"
word = re.search(r"`([^']*)'", your_line).group(1) # good
什么是两者的区别'([^“] *)'VS'' – alvas
他们将有效地匹配相同(*):'[^ '] *'' - 任何东西,但是''',后面跟''','(。*?)' - 最后匹配'''的最小匹配。 – mishik
'\'(。*)'' - 但是,即使匹配行包含引号,它也会匹配'\''和'''之间的任何内容。 – mishik