doubt with positive lookaround

H

hemant

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 (?= ..) ?
 
M

Martin DeMello

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
 
H

hemant

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
 
J

James Edward Gray II

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
 
H

hemant

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top