[Tfug] Regex poser

Chris Niswander MOST.SENDERS.ARE._FILTERED.OUT_--FOR.MY.REAL.EMAIL.ADDRESS.check.my.website..tfug.rcvr.x6a3 at bitboost.com
Thu Nov 5 04:32:40 MST 2009


Chris Niswander wrote:
> 
> Hi, Bexley!
> 
> I think I understand what you want.
> 
> One way of accomplishing this is with
> a tool that has the amusingly non-simple name of
> a 'negative lookbehind assertion'.
> 
> I'll give simpler examples
> that show how to do what you want in Python and Perl.
> 
> Suppose you want the regular expression "[1-3][4-6]"
> but you DO NOT want to match the specific string "25".
> 
> In *Python*,
> 
>  >>> p3 = re.compile("[1-3][4-6](?<!25)")
>  >>> p3.match("14")
> <_sre.SRE_Match object at 0x010B06E8>
>  >>> p3.match("26")
> <_sre.SRE_Match object at 0x010B06B0>
>  >>> p3.match("25")
>  >>> p3.match("35")
> <_sre.SRE_Match object at 0x010B06E8>
>  >>>
> 
> In *Perl*, the negative lookbehind assertion syntax
> also has an equals sign, so I *think* the equivalent
> regex would be [1-3][4-6](?<!=25)
> (I didn't test this!)

On second thought, I don't think you'll want the equals sign in there.

In my defense, I was following
O'Reilly Press' _Perl in a Nutshell_, 1st Ed., page 69,
because I didn't trust my memory at all for the syntax
of these lookbehind and lookahead (which you could also use)
assertions.

So, um, it's O'Reilly's fault. :-)

But I'm on a bit of a deadline,
so why don't you try it, Bexley,
in your code that actually needs it,
and you can tell us how it went. :-)

--Chris




More information about the tfug mailing list