如何在单引号内获取字符串,但忽略“'s”和“'t”?

2024-04-20 14:33:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检索单引号内的字符串,例如在以下句子中:

Play 'My lady's on fire' by Ty Segall

我要检索:

My lady's on fire

我想忽略带有's't的单词,比如“不要”和“女士”:

我试过这个:

re.findall(r"\s\'.*?\'", user_input)

但是,我得到了:

[ 'My lady']

我想得到:

[My lady's on fire]

Tags: 字符串replaybyonmy单词fire
3条回答

根据您的要求,一个备选方案(可能不是最有效的)是:

\'(?:(?!\'[^st]).)*\'

从本质上讲,你一直在使用字符,直到你找到一个以'开头的序列,而不是紧跟着一个st。你知道吗

如果你需要更一般的情况下,你考虑词的边界,然后看看其他答案。你知道吗

^{}

\B assert position where \b does not match
' matches the character ' literally (case sensitive)
Non-capturing group (?:[^']*(?:'\b)?)+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character not present in the list below [^']*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
' matches the character ' literally (case sensitive)
Non-capturing group (?:'\b)?
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
' matches the character ' literally (case sensitive)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
' matches the character ' literally (case sensitive)

您可以使用这个正则表达式->;\b\s'(.*?)(?=\'\s|\'$) 你可以在https://pythex.org/上测试它

Python代码:

import re user_input = "Play 'My lady's on fire' by Nipsey Hussle Play 'My lady's on fire'" print(re.findall(r"\b\s'(.*?)(?=\'\s|\'$)",user_input))

相关问题 更多 >