# Problems with oniguruma lookahead

**URL:** <https://www.ruby-forum.com/t/problems-with-oniguruma-lookahead/148169>\
**Category:** Ruby\
**Created:** [September 10, 2008, 12:51am UTC](https://www.ruby-forum.com/t/problems-with-oniguruma-lookahead/148169 "2008-09-10T00:51:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiongchiamiov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f14d63/32.png) [@xiongchiamiov](https://www.ruby-forum.com/u/xiongchiamiov)\
**Post date:** [September 10, 2008, 12:51am UTC](https://www.ruby-forum.com/t/problems-with-oniguruma-lookahead/148169/1 "2008-09-10T00:51:30Z")

</div>

Ruby 1.8.6 with Oniguruama installed and working (everywhere else, this  
seems to be my problem).

Let me preface this by saying that I am new to Ruby (and kinda jumped  
in, rather than learning it properly), and regexes are not my thing -  
that why I have nifty regex-checkers.

I am trying to extract some parts out of a string  
("

**‘Algebra’**  
") that I scraped from some html. I’m getting  
nil returned from the expression:

Oniguruma::ORegexp.new("(?\<=

**’).\*(?=’**  
)").scan(scraped\_html)

with scraped\_html being the string mentioned above.

Doing some experimenting, I have found that the first part works just as  
planned (eg, everything except the lookahead). Using wildcards (. and  
\*) works as well:

Oniguruma::ORegexp.new("(?\<=

**’).\*(?=.)").scan(scraped\_html)**

returns [#\<MatchData “Foo’\<br”\>, #\<MatchData “Bar’\<br”\>], as  
expected. However, anything else (\<, b, \w, etc.) causes the regex to  
not match.

I am quite befuddled about this, though I (almost certainly) know it is  
my fault. Any help would be much appreciated.

Also, if I am violating any mailing-list netiquette, I would like to  
know as well.

---

<div class="post-metadata">

**Author:** ![xiongchiamiov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f14d63/32.png) [@xiongchiamiov](https://www.ruby-forum.com/u/xiongchiamiov)\
**Post date:** [September 10, 2008, 11:13am UTC](https://www.ruby-forum.com/t/problems-with-oniguruma-lookahead/148169/2 "2008-09-10T11:13:54Z")

</div>

2008/9/10 Xiong C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> returns [#\<MatchData “Foo’\<br”\>, #\<MatchData “Bar’\<br”\>], as  
> expected. However, anything else (\<, b, \w, etc.) causes the regex to  
> not match.
> 
> I am quite befuddled about this, though I (almost certainly) know it is  
> my fault. Any help would be much appreciated.

With 1.9:

irb(main):001:0\> s=“

**‘Algebra’**  
”  
=\> “

**‘Algebra’**  
”  
irb(main):002:0\> s.scan %r{(?\<=

**‘)._(?=‘_**  
_)}  
=\> []  
irb(main):003:0\> s.scan %r{(?\<=_

_**’).**_**?(?=’**  
)}  
=\> [“Algebra”]

Note the non greedy match. I usually rather do this in those cases:

irb(main):005:0\> s.scan %r{

**‘(.\*?)’**  
}  
=\> [[“Algebra”]]

I.e. use groups to extract the part that I am interested in.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![xiongchiamiov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f14d63/32.png) [@xiongchiamiov](https://www.ruby-forum.com/u/xiongchiamiov)\
**Post date:** [September 10, 2008, 5:59pm UTC](https://www.ruby-forum.com/t/problems-with-oniguruma-lookahead/148169/3 "2008-09-10T17:59:46Z")

</div>

Robert K. wrote:

> With 1.9:
> 
> irb(main):001:0\> s="
> 
> **‘Algebra’**  
> "  
> =\> “
> 
> **‘Algebra’**  
> ”  
> irb(main):002:0\> s.scan %r{(?\<=
> 
> **’)._(?=’_**  
> )}  
> =\> []  
> irb(main):003:0\> s.scan %r{(?\<=
> 
> **’).?(?=’**  
> )}  
> =\> [“Algebra”]
> 
> Note the non greedy match. I usually rather do this in those cases:
> 
> irb(main):005:0\> s.scan %r{
> 
> **’(.\*?)’**  
> }  
> =\> [[“Algebra”]]
> 
> I.e. use groups to extract the part that I am interested in.
> 
> Kind regards
> 
> robert

Ah, thank you very much. My regex learning was with PHP (PCRE, not  
POSIX), which has some odd rules, especially regarding  
greedy/non-greedy, so I’m still trying to recover from that.

Thanks again.
