a little about regex

Rob Wolfe blue99 at interia.pl
Wed Oct 18 04:43:10 EDT 2006


Fulvio wrote:

> I'm trying to get working an assertion which filter address from some domain
> but if it's prefixed by '.com'.
> Even trying to put the result in a negate test I can't get the wanted result.

[...]

> Seem that I miss some better regex implementation to avoid that both of the
> filters taking action. I'm thinking of lookbehind (negative or positive)
> option, but I think I couldn't realize it yet.
> I think the compilation should either allow have no '.com' before '.my' or
> deny should have _only_ '.com' before '.my'. Sorry I don't get the correct
> sintax to do it.
>
> Suggestions are welcome.

Try this:

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


HTH,
Rob




More information about the Python-list mailing list