doubt with positive lookaround

Discussion in 'Ruby' started by hemant, Dec 5, 2006.

  1. hemant

    hemant Guest

    I do not understand behavior of positive look arounds in Ruby:

    chunked_data = "DIVex>##ECHO=b698fd4cc1ea4c7a4b5c4e1d1c402911##"
    p $& if chunked_data =~ /(?=DIVex)DI/ # => DI

    However,

    chunked_data = "<DIVex>##ECHO=b698fd4cc1ea4c7a4b5c4e1d1c402911##"
    p $& if chunked_data =~ /(?=<DIVex)DI/ # => nil


    does < gets treated as special character inside (?= ..) ?






    --
    There was only one Road; that it was like a great river: its springs
    were at every doorstep, and every path was its tributary.
     
    hemant, Dec 5, 2006
    #1
    1. Advertising

  2. On 12/5/06, hemant <> wrote:
    > I do not understand behavior of positive look arounds in Ruby:
    >
    > chunked_data = "DIVex>##ECHO=b698fd4cc1ea4c7a4b5c4e1d1c402911##"
    > p $& if chunked_data =~ /(?=DIVex)DI/ # => DI
    >
    > However,
    >
    > chunked_data = "<DIVex>##ECHO=b698fd4cc1ea4c7a4b5c4e1d1c402911##"
    > p $& if chunked_data =~ /(?=<DIVex)DI/ # => nil
    >
    >
    > does < gets treated as special character inside (?= ..) ?


    No, but lookahead assertions are zero-width, so if your assertion
    matches <DIV, your cursor is still before the <, and thus won't match
    DI. p $& if chunked_data =~ /(?=<DIVex)<DI/ works fine.

    martin
     
    Martin DeMello, Dec 5, 2006
    #2
    1. Advertising

  3. hemant

    hemant Guest

    On 12/5/06, Martin DeMello <> wrote:
    > On 12/5/06, hemant <> wrote:
    > > I do not understand behavior of positive look arounds in Ruby:
    > >
    > > chunked_data = "DIVex>##ECHO=b698fd4cc1ea4c7a4b5c4e1d1c402911##"
    > > p $& if chunked_data =~ /(?=DIVex)DI/ # => DI
    > >
    > > However,
    > >
    > > chunked_data = "<DIVex>##ECHO=b698fd4cc1ea4c7a4b5c4e1d1c402911##"
    > > p $& if chunked_data =~ /(?=<DIVex)DI/ # => nil
    > >
    > >
    > > does < gets treated as special character inside (?= ..) ?

    >
    > No, but lookahead assertions are zero-width, so if your assertion
    > matches <DIV, your cursor is still before the <, and thus won't match
    > DI. p $& if chunked_data =~ /(?=<DIVex)<DI/ works fine.


    Holy cat...thanks



    --
    There was only one Road; that it was like a great river: its springs
    were at every doorstep, and every path was its tributary.
     
    hemant, Dec 5, 2006
    #3
  4. On Dec 5, 2006, at 12:58 PM, wrote:

    > There are a few types of "lookarounds": positive lookahead,
    > negative lookahead, positve lookbehind, negative look behind.
    >
    > To use positive lookbehind, you would use (?<=...). Ruby might be
    > interpreting ?=< as ?<=. Try escaping the < by adding a \ before it.


    The regex engine in Ruby 1.8 does not support look-behind
    assertions. The regex engine in 1.9, Oniguruma, does.

    James Edward Gray II
     
    James Edward Gray II, Dec 5, 2006
    #4
  5. hemant

    hemant Guest

    On 12/6/06, James Edward Gray II <> wrote:
    > On Dec 5, 2006, at 12:58 PM, wrote:
    >
    > > There are a few types of "lookarounds": positive lookahead,
    > > negative lookahead, positve lookbehind, negative look behind.
    > >
    > > To use positive lookbehind, you would use (?<=...). Ruby might be
    > > interpreting ?=< as ?<=. Try escaping the < by adding a \ before it.

    >
    > The regex engine in Ruby 1.8 does not support look-behind
    > assertions. The regex engine in 1.9, Oniguruma, does.



    Yup James I am aware of that. I was blind enough to not see my silly mistake.



    Thanks


    --
    There was only one Road; that it was like a great river: its springs
    were at every doorstep, and every path was its tributary.
     
    hemant, Dec 6, 2006
    #5
    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. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    635
  2. Hicham Mouline
    Replies:
    2
    Views:
    780
    Keith Thompson
    Apr 23, 2010
  3. debraj

    lookaround

    debraj, Jul 6, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    91
    Tad McClellan
    Jul 6, 2003
  4. Replies:
    0
    Views:
    570
  5. Peter Otten
    Replies:
    2
    Views:
    125
    Cousin Stanley
    Aug 10, 2013
Loading...

Share This Page