Re: help: negative lookahead and backref in regex?
From: Scott Panzer (stenor_at_bayarea.net)
Date: 12/29/04
- Next message: Dave Seaman: "Re: Is zero even or odd?"
- Previous message: Dave Seaman: "Re: Is zero even or odd?"
- In reply to: Gunnar Hjalmarsson: "Re: help: negative lookahead and backref in regex?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 28 Dec 2004 17:20:19 -0800
Thanks Gunnar, this is very helpful.
On Wed, 29 Dec 2004, Gunnar Hjalmarsson wrote:
> stenor@bayarea.net wrote:
> > I'm trying to match a pattern that looks more or less like this:
> >
> > <string1><statictext><string2>
> >
> > where string1 and string2 are not the same. I need to do this in a
> > single regex (rather than capturing $1 and $2 and comparing after the
> > fact.
> >
> > I've tried this as a test (in this case <statictext> is just
> > whitespace:
> >
> > /(\w+)\s+(?!\1)/
> >
> > This will match "foo bar" as expected, but it also matches "foo foo"
> > which it ought not to.
>
> No, it doesn't. It matches "oo foo", which it ought to. ;-) Did you try
> to print the $1 variable?
>
> <snip>
>
> > Any ideas regarding why using the negative lookahead assertion as I've
> > done doesn't do what I'd like it to do? And/or how to do it correctly?
>
> Express yourself more clearly, by for instance adding a word boundary
> character. This matches "foo bar" but not "foo foo":
>
> /\b(\w+)\s+(?!\1)/
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>
--------------------------------------------------------------------
Scott Panzer http://www.bayarea.net/~stenor Sunnyvale, CA
PGP public keys found available at my web site
--------------------------------------------------------------------
- Next message: Dave Seaman: "Re: Is zero even or odd?"
- Previous message: Dave Seaman: "Re: Is zero even or odd?"
- In reply to: Gunnar Hjalmarsson: "Re: help: negative lookahead and backref in regex?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]