Python匹配字符串,如果它不以X开头

Joh*_*alt 5 python regex match

我想在我的磁盘上搜索一个名为"AcroTray.exe"的文件.如果文件位于"Distillr"以外的目录中,程序应该打印警告.我使用以下语法来执行否定匹配

(?!Distillr)
Run Code Online (Sandbox Code Playgroud)

问题是虽然我使用"!" 它总是产生一个MATCH.我试图使用IPython找出问题,但失败了.这是我试过的:

import re

filePath = "C:\Distillr\AcroTray.exe"

if re.search(r'(?!Distillr)\\AcroTray\.exe', filePath):
    print "MATCH"
Run Code Online (Sandbox Code Playgroud)

它打印一个MATCH.我的正则表达式出了什么问题?

我想得到一个匹配:

C:\SomeDir\AcroTray.exe
Run Code Online (Sandbox Code Playgroud)

但不是:

C:\Distillr\AcroTray.exe
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 1

使用负lookbehind ( (?<!...)),而不是负lookahead:

if re.search(r'(?<!Distillr)\\AcroTray\.exe', filePath):
Run Code Online (Sandbox Code Playgroud)

这匹配:

In [45]: re.search(r'(?<!Distillr)\\AcroTray\.exe', r'C:\SomeDir\AcroTray.exe')
Out[45]: <_sre.SRE_Match at 0xb57f448>
Run Code Online (Sandbox Code Playgroud)

这不匹配:

In [46]: re.search(r'(?<!Distillr)\\AcroTray\.exe', r'C:\Distillr\AcroTray.exe')
# None
Run Code Online (Sandbox Code Playgroud)