Python matching string if it does not start with X

I want to find a file called "AcroTray.exe" on my disk. The program should print a warning if the file is in a directory other than "Distillr" . I used the following syntax to do a negative match

(?!Distillr)

The problem is that although I use "!" he always produces MATCH. I tried to find out the problem using IPython but could not. This is what I tried:

import re

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

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

He is typing MATCH. What is wrong with my regex?

I would like to get a match:

C:\SomeDir\AcroTray.exe

But not on:

C:\Distillr\AcroTray.exe
+5
source share
4 answers

lookbehind ((?<!...)), :

if re.search(r'(?<!Distillr)\\AcroTray\.exe', filePath):

:

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

:

In [46]: re.search(r'(?<!Distillr)\\AcroTray\.exe', r'C:\Distillr\AcroTray.exe')
# None
+1

: (?<!Distillr)\\AcroTray\.exe

0

, . :

(?<!Distillr)\\AcroTray\.exe
0

(?mx)^((?!Distillr).)*$

Looking at the examples you provided, I use them as examples here

0
source

All Articles