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

时间:2019-04-28 10:14:31

标签: python regex

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

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]

3 个答案:

答案 0 :(得分:3)

https://github.com/endeneer1/google-form-autofilling-spam-bot-using-Python-multiple-choice-questions/blob/master/google-form-spambot.py

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

答案 1 :(得分:1)

您可以使用此正则表达式---> \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))

答案 2 :(得分:0)

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

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

从本质上讲,您一直在使用字符,直到找到一个以'开头且后面没有 s t 的序列。

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