# Doubt with positive lookaround

**URL:** <https://rubytalk.org/t/doubt-with-positive-lookaround/33460>\
**Category:** ruby-talk\
**Created:** [5 December 2006 04:28 UTC](https://rubytalk.org/t/doubt-with-positive-lookaround/33460 "2006-12-05T04:28:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)\
**Post date:** [5 December 2006 04:28 UTC](https://rubytalk.org/t/doubt-with-positive-lookaround/33460/1 "2006-12-05T04:28:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [5 December 2006 04:38 UTC](https://rubytalk.org/t/doubt-with-positive-lookaround/33460/2 "2006-12-05T04:38:26Z")

</div>

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

> **···**
>
> On 12/5/06, hemant \<gethemant@gmail.com\> 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 (?= ..) ?

---

<div class="post-metadata">

**Author:** ![Daniel\_Finnie1](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@Daniel\_Finnie1](https://rubytalk.org/u/Daniel_Finnie1)\
**Post date:** [5 December 2006 18:58 UTC](https://rubytalk.org/t/doubt-with-positive-lookaround/33460/3 "2006-12-05T18:58:08Z")

</div>

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.

> **···**
>
> ----- Original Message -----  
> From: hemant  
> Date: Monday, December 4, 2006 11:29 pm  
> Subject: doubt with positive lookaround  
> To: ruby-talk@ruby-lang.org (ruby-talk ML)
> 
> > 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 = "##ECHO=b698fd4cc1ea4c7a4b5c4e1d1c402911##"  
> > p $& if chunked\_data =~ /(?= 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.

---

<div class="post-metadata">

**Author:** ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)\
**Post date:** [5 December 2006 04:56 UTC](https://rubytalk.org/t/doubt-with-positive-lookaround/33460/4 "2006-12-05T04:56:21Z")

</div>

Holy cat...thanks

> **···**
>
> On 12/5/06, Martin DeMello \<martindemello@gmail.com\> wrote:
> 
> > On 12/5/06, hemant \<gethemant@gmail.com\> 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.
> 
> --  
> There was only one Road; that it was like a great river: its springs  
> were at every doorstep, and every path was its tributary.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [5 December 2006 19:05 UTC](https://rubytalk.org/t/doubt-with-positive-lookaround/33460/5 "2006-12-05T19:05:28Z")

</div>

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

> **···**
>
> On Dec 5, 2006, at 12:58 PM, danfinnie@optonline.net 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.

---

<div class="post-metadata">

**Author:** ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)\
**Post date:** [6 December 2006 06:07 UTC](https://rubytalk.org/t/doubt-with-positive-lookaround/33460/6 "2006-12-06T06:07:28Z")

</div>

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

Thanks

> **···**
>
> On 12/6/06, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Dec 5, 2006, at 12:58 PM, danfinnie@optonline.net 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.
> 
> --  
> There was only one Road; that it was like a great river: its springs  
> were at every doorstep, and every path was its tributary.
