我想检索单引号内的字符串,例如在句子中:
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]
答案 0 :(得分:3)
\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 的序列。
如果您需要考虑词边界的更一般的情况,请查看其他答案。