Python 排除并包含两个字符串的正则表达式匹配字符串
以两个字符串为例,例如aaaa和bbbb,我希望a找到一个正则表达式,它将包含aaaa的每个字符串作为子字符串进行数学运算,但同时不包含bbbbPython 排除并包含两个字符串的正则表达式匹配字符串,python,regex,Python,Regex,以两个字符串为例,例如aaaa和bbbb,我希望a找到一个正则表达式,它将包含aaaa的每个字符串作为子字符串进行数学运算,但同时不包含bbbb test = ['staaaap', 'nomatchaaaaabbbb', 'matchyaaaabbb'] for s in test: m = re.match(r'(?!.*bbbb.*).*aaaa.*', s) if m: print('{} match: {}'.format(s, m.group(0))
test = ['staaaap', 'nomatchaaaaabbbb', 'matchyaaaabbb']
for s in test:
m = re.match(r'(?!.*bbbb.*).*aaaa.*', s)
if m:
print('{} match: {}'.format(s, m.group(0)))
else:
print('{} no match'.format(s))
输出:
staaaap match: staaaap
nomatchaaaaabbbb no match
matchyaaaabbb match: matchyaaaabbb
r'^(?。*bbbb)。*aaaa'