# Doubt with positive lookaround

**URL:** <https://www.ruby-forum.com/t/doubt-with-positive-lookaround/78342>\
**Category:** Ruby\
**Created:** [December 5, 2006, 5:30am UTC](https://www.ruby-forum.com/t/doubt-with-positive-lookaround/78342 "2006-12-05T05:30:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [December 5, 2006, 5:30am UTC](https://www.ruby-forum.com/t/doubt-with-positive-lookaround/78342/1 "2006-12-05T05:30:09Z")

</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 = “##ECHO=b698fd4cc1ea4c7a4b5c4e1d1c402911##”  
p $& if chunked\_data =~ /(?=\<DIVex)DI/ # =\> nil

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

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [December 5, 2006, 5:40am UTC](https://www.ruby-forum.com/t/doubt-with-positive-lookaround/78342/2 "2006-12-05T05:40:10Z")

</div>

On 12/5/06, hemant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [December 5, 2006, 5:58am UTC](https://www.ruby-forum.com/t/doubt-with-positive-lookaround/78342/3 "2006-12-05T05:58:12Z")

</div>

On 12/5/06, Martin DeMello [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 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

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [December 5, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/doubt-with-positive-lookaround/78342/4 "2006-12-05T20:00:24Z")

</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: removed\_email\_address@domain.invalid (ruby-talk ML)

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [December 5, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/doubt-with-positive-lookaround/78342/5 "2006-12-05T20:06:25Z")

</div>

On Dec 5, 2006, at 12:58 PM, removed\_email\_address@domain.invalid 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 G. II

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [December 6, 2006, 7:08am UTC](https://www.ruby-forum.com/t/doubt-with-positive-lookaround/78342/6 "2006-12-06T07:08:25Z")

</div>

On 12/6/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Dec 5, 2006, at 12:58 PM, removed\_email\_address@domain.invalid 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
