【发布时间】:2021-08-15 15:18:50
【问题描述】:
我有这个检测所有单词的正则表达式:
\b[^\d\W]+\b
我有这个正则表达式来检测引用的文本:
\'[^\".]*?\'|\"[^\'.]*?\"
是否有正则表达式可以检测不在引号中的单词(单引号和双引号)?
示例:
import re
a = "big mouse eats cheese? \"non-detected string\" 'non-detected string too' hello guys"
re.findall(some_regex, a)
它应该输出这个
['big', 'mouse', 'eats', 'cheese', 'hello', 'guys']
我知道我可以使用re.sub() 来检测引用的文本,然后将其替换为空白字符串,但这就是我不想做的事情。
我还查看了此页面regex match keywords that are not in quotes 并尝试了此(^([^"]|"[^"]*")*)|(^([^']|'[^']*')*) 但它不起作用A regex to detect string not enclosed in double quotes 也尝试了此(?<![\S"])([^"\s]+)(?![\S"])|(?<![\S'])([^'\s]+)(?![\S']) 都检测到所有单词
【问题讨论】:
标签: python-3.x regex quotes