繁体   English   中英

如何获取正则表达式在“单词”之间的单词边界? 蟒蛇

[英]How to get a word bounded between `word' with regex? python

给定字符串

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM