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

标签 python regex

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

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/

相关文章:

python - 我怎么弄乱了python pdb

java.util.regex.PatternSyntaxException : Unclosed character class near index 12\\b]([^. (|[]+)

regex - 匹配基本 url 正则表达式

c# - 正则表达式:找出我的匹配项是否在 <span> 中

python - 如何从图像中去除显示器闪烁的噪音?

python - 我正在尝试使用 SQLAlchemy 作为 db.session 在 Flask 应用程序中表示 sql 连接

regex - 匹配单词之前或之后的字符,但不能同时匹配正则表达式

java - Sed 用另一个文本替换多行 Java 注释

python - 提取所有文档字符串的行号?

python - Xpath - 获取 2 个节点,其中 1 个节点如果缺失则具有默认值