Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用正则表达式匹配字符中不包含的单词_Python_Regex_Regex Negation - Fatal编程技术网

Python 使用正则表达式匹配字符中不包含的单词

Python 使用正则表达式匹配字符中不包含的单词,python,regex,regex-negation,Python,Regex,Regex Negation,我试图匹配不在里面的单词 这是内部匹配单词的正则表达式: 我希望结果为['Hi'、'is'、'go']。使用: 正则表达式方法: >>> import re >>> re.findall(r"\b(?<!<)\w+(?!>)\b", text) ['Hi', 'is', 'going'] 为了处理你是谁的情况,我们需要一些不同的东西: >>> text = " Hi <how> is <everythin

我试图匹配不在里面的单词

这是内部匹配单词的正则表达式:

我希望结果为['Hi'、'is'、'go']。

使用:


正则表达式方法:

>>> import re
>>> re.findall(r"\b(?<!<)\w+(?!>)\b", text)
['Hi', 'is', 'going']
为了处理你是谁的情况,我们需要一些不同的东西:

>>> text = " Hi <how> is <everything> going"
>>> re.findall(r"(?:^|\s)(?!<)([\w\s]+)(?!>)(?:\s|$)", text)
[' Hi', 'is', 'going']
>>> text = "<hello how> are you"
>>> re.findall(r"(?:^|\s)(?!<)([\w\s]+)(?!>)(?:\s|$)", text)
['are you']

请注意,您现在必须被拆分以获得单个单词。

单词Hi、is和going,对不起,在前面总是有空格吗?仅供参考,没有特殊意义,不需要转义。如果单词之间有空格,例如,您和我是否要提取are和you?哦,doc=你匹配吗=关于findallr\b\\b、 博士,这段摘录['hello','are','you']@Inigo好的,请看更新。希望这有帮助。@Inigo谢谢,我仍然有一种强烈的感觉,那就是有一种更简单更好的方法。
>>> import re
>>> re.findall(r"\b(?<!<)\w+(?!>)\b", text)
['Hi', 'is', 'going']
>>> [word for word in text.split() if not word.startswith("<") and not word.endswith(">")]
['Hi', 'is', 'going']
>>> text = " Hi <how> is <everything> going"
>>> re.findall(r"(?:^|\s)(?!<)([\w\s]+)(?!>)(?:\s|$)", text)
[' Hi', 'is', 'going']
>>> text = "<hello how> are you"
>>> re.findall(r"(?:^|\s)(?!<)([\w\s]+)(?!>)(?:\s|$)", text)
['are you']