help: negative lookahead and backref in regex?

Discussion in 'Perl Misc' started by stenor@bayarea.net, Dec 28, 2004.

  1. Guest

    I'd appreciate some help from a regex guru with this:

    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.

    Using positive lookahead had the expected result:
    /(\w+)\s+(?=\1)/ matches "foo foo" but not "foo bar", which is the
    intended behavior.

    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?
    email cc appreciated.

    Thanks,
    Scott
    , Dec 28, 2004
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Dec 28, 2004
    #2
    1. Advertising

  3. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > I'd appreciate some help from a regex guru with this:
    >
    > 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.


    It matches "oo", followed by a blank, followed by something that is not
    "oo" (namely "foo"), as it ought to. You may want to anchor the pattern,
    or use "\b" boundaries around parts.

    Anno
    Anno Siegel, Dec 29, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jelle Smet
    Replies:
    2
    Views:
    730
    Helmut Jarausch
    Nov 23, 2009
  2. MRAB
    Replies:
    0
    Views:
    889
  3. Phrogz
    Replies:
    2
    Views:
    123
    William James
    Feb 19, 2005
  4. Dan Rawson

    Regex backref returns extra data

    Dan Rawson, Sep 25, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    142
    Uri Guttman
    Sep 25, 2003
  5. shifty
    Replies:
    9
    Views:
    197
Loading...

Share This Page