繁体   English   中英

使用正则表达式查找字符串中除单词中的所有单词以外的所有单词

[英]Find all the words in string except one word in python with regex

我正在python中使用正则表达式,我想搜索字符串中除一个单词以外的所有单词。 码:

import re
string = "The world is too big"
print re.findall("regex", string)

如果我想获得除单词“ too”以外的所有单词(因此输出将为["The", "world", "is", "big"] ),如何在正则表达式中实现呢?

您甚至不需要为此使用正则表达式,只需使用splitfilter

sentence = "The world is too big"
sentence = list(filter(lambda x: x != 'too', sentence.split()))
print(sentence)

删除字符串中的“太”,然后分割字符串。

re.sub(r'\btoo\b','',string).split()
Out[15]: ['The', 'world', 'is', 'big']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM