2

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

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]
4

3 回答 3

3

\B'(?:[^']*(?:'\b)?)+'

\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)
于 2019-04-28T11:02:33.687 回答
1

您可以使用此正则表达式 --- >\b\s'(.*?)(?=\'\s|\'$) 您可以在https://pythex.org/上对其进行测试

蟒蛇代码:

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))

于 2019-04-28T11:16:14.643 回答
0

根据您的要求,一种替代方案(可能不是性能最高的)是这个:

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

从本质上讲,您正在获取字符,直到您找到一个以st'开头但不跟在其后的序列。

如果您需要考虑单词边界的更一般情况,请查看其他答案。

于 2019-04-28T12:08:49.673 回答