import re
def xstring(substring, words):
regex = re.compile('[%s]' % ''.join(sorted(set(substring))))
# Excluding words matching regex.pattern
for word in words:
if not re.search(regex, word):
print(word)
words = [
'nana',
'abab',
'nanac',
'eded',
]
xstring("bn", words)
import re
substring = "bn"
regex = re.compile('[' + substring + ']')
def xstring():
with open("dename.txt") as f:
for line in f:
if(re.search(regex, line) is None):
print(line)
xstring()
@Xosrov有正确的方法,有一些小问题和错误。以下版本的同一逻辑起作用
这里,我们只需要一个简单的表达式,例如:
我们在not char类},并收集所有其他字符,然后通过添加}锚定,我们将使所有可能具有}的字符串失败。在
[^bn]
中添加b
和{^
和{b
和{Demo
测试
^{pr2}$正则表达式
如果不需要这个表达式,可以在regex101.com中修改/更改它。在
正则表达式电路
jex.im可视化正则表达式:
如果要检查字符串是否包含一组字母,请使用括号。
例如,使用
[bn]
将匹配包含其中一个字母的单词。在相关问题 更多 >
编程相关推荐