Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > doubt with positive lookaround

Reply
Thread Tools

doubt with positive lookaround

 
 
hemant
Guest
Posts: n/a
 
      12-05-2006
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=b698fd4cc1ea4c7a4b5c4e1d1c40291 1##"
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.

 
Reply With Quote
 
 
 
 
Martin DeMello
Guest
Posts: n/a
 
      12-05-2006
On 12/5/06, hemant <(E-Mail Removed)> 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=b698fd4cc1ea4c7a4b5c4e1d1c40291 1##"
> 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

 
Reply With Quote
 
 
 
 
hemant
Guest
Posts: n/a
 
      12-05-2006
On 12/5/06, Martin DeMello <(E-Mail Removed)> wrote:
> On 12/5/06, hemant <(E-Mail Removed)> 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=b698fd4cc1ea4c7a4b5c4e1d1c40291 1##"
> > 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.

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      12-05-2006
On Dec 5, 2006, at 12:58 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
hemant
Guest
Posts: n/a
 
      12-06-2006
On 12/6/06, James Edward Gray II <(E-Mail Removed)> wrote:
> On Dec 5, 2006, at 12:58 PM, (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
floor(positive double) vs trunc(positive double) different Hicham Mouline C Programming 2 04-23-2010 06:50 PM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
Help simplify complex regexp needing positive lookahead and reluctant quantifers david.karr@wamu.net Java 7 03-25-2005 09:05 PM
lookaround debraj Perl Misc 3 07-06-2003 02:08 PM



Advertisments