a little about regex

Ant antroy at gmail.com
Wed Oct 18 11:05:24 EDT 2006


Rob Wolfe wrote:
...
> def filter(adr):    # note that "filter" is a builtin function also
>     import re
>
>     allow = re.compile(r'.*(?<!\.com)\.my(>|$)')  # negative lookbehind
>     deny = re.compile(r'.*\.com\.my(>|$)')
>     cnt = 0
>     if deny.search(adr): cnt += 1
>     if allow.search(adr): cnt += 1
>     return cnt

Which makes the 'deny' code here redundant so in this case the function
could be reduced to:

import re

def allow(adr):    # note that "filter" is a builtin function also
    allow = re.compile(r'.*(?<!\.com)\.my(>|$)')  # negative lookbehind
    if allow.search(adr):
        return True
    return False

Though having the explicit allow and deny expressions may make what's
going on clearer than the fairly esoteric negative lookbehind.




More information about the Python-list mailing list