我想检索单引号内的字符串,例如在句子中:
Play 'My lady's on fire' by Ty Segall
我想检索:
My lady's on fire
我想忽略带有 's
和 't
的单词,例如“don't”和“lady's”:
我试过这个:
re.findall(r"\s\'.*?\'", user_input)
但是,我得到:
[ 'My lady']
我想要得到:
[My lady's on fire]
最佳答案
\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)
关于python - 如何获取单引号内的字符串但忽略 "' s "and " 't "?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55889226/