RegExp Look for part but exclude If
Question: Right so RegExp is fairly new to me and its still puzzling me. Anyway I
managed to look for almost all the names I want to but now I have to look for
names that have specific part of world in it but exclude if there is another
one next to it.

So basically

Looks for `"Cat"` however if there is `"Dog"` then exclude this string so a:

    
    
    CatBlablablaDog = exclude
    blablaCatBlabla = include
    Dog_blabla_Cat = exclude
    

Atm I'm using this to look for my names `'*Cat*'` I guess I could also use
`/*.Cat/` or something similar to that(I cant remember exact symbols I have it
somewhere written) So what do I have to add to exclude the Dog objects? (I
code in Python)

If I cant do it this way maybe I can loop through objects and look for other
names than Dog. Then iterate through the new non dog names for Cat names?
Which mean what do I type to not look for Dog name? :)

Thanks, bye.


Answer: Use negative look-ahead:

    
    
    ^(?!.*dog).*cat.*$
    

It will first test that there is no `dog` further in the string. If negative
look-ahead succeeds, then it goes on to match the string containing `cat`. You
might want to enable ignore case in it using `(?i)` flag.

In python, you can also use `re.IGNORECASE`:

    
    
    >>> import re
    >>> ignorecase = re.compile(r'(?i)(?!.*dog).*cat.*')
    >>> print ignorecase.match("CatBlablablaDog")
    None
    >>>
    >>> print ignorecase.match("blablaCatBlabla")
    <_sre.SRE_Match object at 0x956b090>
    >>>
    >>> print ignorecase.match("Dog_blabla_Cat")
    None
    

