gpt4 book ai didi

python - 如何获取单引号内的字符串但忽略 "' s "and " 't "?

转载 作者:行者123 更新时间:2023-11-28 22:11:16 27 4
gpt4 key购买 nike

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

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'(?:[^']*(?:'\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)

关于python - 如何获取单引号内的字符串但忽略 "' s "and " 't "?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55889226/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com