# Oniguruma lookbehind question

**URL:** <https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639>\
**Category:** Ruby\
**Created:** [January 7, 2006, 3:58am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639 "2006-01-07T03:58:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 3:58am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/1 "2006-01-07T03:58:56Z")

</div>

Hi –

For some reason, lookbehind and alternation seem not to be playing  
together in a little Oniguruma test. This is based on the string  
splitting thread from a little while ago this evening, and uses a CVS  
1.9.0 Ruby acquired about 1/2 an hour ago.

str = %Q{abc def “ghi jkl” mno}

# Look for “…” but just get the … part:

re1 = /(?\<=“)[^”]+(?=")/

# Test that:

p str.scan(re1) # =\> [“ghi jkl”]

# Now, do the same thing _or_ \S+. This should, I think,

# pick up the abc, def, and mno substrings too.

re2 = /((?\<=“)[^”]+(?="))|(\S+)/

# But it doesn’t; the part before the alternation never

# matches, even though it did before (as shown by the

# captures):

p str.scan(re2)

# =\> [[nil, “abc”], [nil, “def”], [nil, “"ghi”], [nil, “jkl"”],

# [nil, “mno”]]

I know that’s all a bit cluttered, but the basic thing is that a  
sub-pattern using lookbehind doesn’t seem to match any more when  
there’s an alternation. Instead, only the second alternative ever  
matches.

Does anyone know why?

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 4:28am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/2 "2006-01-07T04:28:57Z")

</div>

removed\_email\_address@domain.invalid wrote:

> # Test that:
> 
> p str.scan(re2)
> 
> # =\> [[nil, “abc”], [nil, “def”], [nil, ““ghi”], [nil, “jkl””],
> 
> # [nil, “mno”]]
> 
> I know that’s all a bit cluttered, but the basic thing is that a  
> sub-pattern using lookbehind doesn’t seem to match any more when  
> there’s an alternation. Instead, only the second alternative ever  
> matches.

Is this pattern work for you?

str = %Q{abc def “ghi jkl” mno}  
re3 = /((?\<=")[^"]+(?="))|([\S&&[^"]]+)/  
p str.scan(re3) #=\> [[nil, “abc”], [nil, “def”], [“ghi jkl”, nil],  
[nil, “mno”]]

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 4:43am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/3 "2006-01-07T04:43:57Z")

</div>

On Sat, 07 Jan 2006 02:56:50 -0000, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> re1 = /(?\<=“)[^”]+(?=")/
> 
> # matches, even though it did before (as shown by the
> 
> Does anyone know why?

I’m not at all sure about this, but this is my take on it. Firstly, is  
this the behaviour you expected?

```
str = %Q{abc def "ghi jkl" mno}
re2 = /(?:(?<=")[^"]+(?="))|\S+/

p str.scan(re2)
# => ["abc", "def", "\"ghi", "jkl\"", "mno"]

```

?

If so, then I believe the problem is something to do with the fact that  
lookaround is atomic, so that when used with capturing groups and  
alternations you sometimes experience problems because the regex  
immediately forgets the (zero-width, remember) lookaround match, so that  
by the time it comes to that ‘or’ it doesn’t have the information to  
compare.

Generally, there are restrictions with lookaround (esp lookbehind)  
matching, and especially when matching regexps. So far my experiments  
with  
Oniguruma suggest it’s fairly sophisticated in this respect, supporting  
stuff like varying-width alternations, fixed repetition and optional  
groups in lookbehind, but of course still no star and plus.

Anyway, that’s what I think. Hope it helps 🙂

Cheers,

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 4:49am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/4 "2006-01-07T04:49:58Z")

</div>

On Sat, 07 Jan 2006 03:36:34 -0000, Ross B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Generally, there are restrictions with lookaround (esp lookbehind)  
> matching, and especially when matching regexps.

Oops, I mean nested regexps.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 4:52am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/5 "2006-01-07T04:52:58Z")

</div>

On Jan 7, 2006, at 3:56, removed\_email\_address@domain.invalid wrote:

> # Now, do the same thing _or_ \S+. This should, I think,
> 
> # pick up the abc, def, and mno substrings too.
> 
> re2 = /((?\<=")[^"]+(?="))|(\S+)/
> 
> # But it doesn’t; the part before the alternation never
> 
> # matches,

It shouldn’t, since pattern-matching goes left-to-right \S will match  
the quote before the first half of the regexp gets a chance, since it  
wants to match the first character _after_ the quote.

– fxn

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 4:52am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/6 "2006-01-07T04:52:58Z")

</div>

Hi –

On Sat, 7 Jan 2006, K.Kosako wrote:

> > re1 = /(?\<=“)[^”]+(?=")/
> > 
> > # matches, even though it did before (as shown by the
> 
> Is this pattern work for you?
> 
> str = %Q{abc def “ghi jkl” mno}  
> re3 = /((?\<=“)[^”]+(?=“))|([\S&&[^”]]+)/  
> p str.scan(re3) #=\> [[nil, “abc”], [nil, “def”], [“ghi jkl”, nil], [nil,  
> “mno”]]

No; I get this:

[[nil, “"”], [“ghi jkl”, nil], [nil, “"”]]

This:

/(?\<=“)[^”]+(?=“)|[^\s”]+/

gives me the result you got for your re3. But it still seems to be  
based on the right-hand alternate being checked first.

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 5:10am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/7 "2006-01-07T05:10:59Z")

</div>

Hi –

On Sat, 7 Jan 2006, Xavier N. wrote:

> It shouldn’t, since pattern-matching goes left-to-right \S will match the  
> quote before the first half of the regexp gets a chance, since it wants to  
> match the first character _after_ the quote.

OK, I see. I was somehow discounting the fact that the first "  
_itself_ doesn’t match the left-hand alternate.

Thanks –

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 5:10am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/8 "2006-01-07T05:10:59Z")

</div>

On Sat, 07 Jan 2006 03:36:34 -0000, Ross B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If so, then I believe the problem is something to do with the fact that  
> lookaround is atomic

Argh, I meant ‘if not’. I’m too tired now, I’ve spent too long  
perfecting  
this quiz thing…  
I was guessing it was dropping the match without the capture and so  
always  
matching the alternate but that’s not right.

Sorry.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 5:13am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/9 "2006-01-07T05:13:59Z")

</div>

Hi –

On Sat, 7 Jan 2006, Ross B. wrote:

> On Sat, 07 Jan 2006 03:36:34 -0000, Ross B.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > If so, then I believe the problem is something to do with the fact that  
> > lookaround is atomic
> 
> Argh, I meant ‘if not’. I’m too tired now, I’ve spent too long perfecting  
> this quiz thing…  
> I was guessing it was dropping the match without the capture and so always  
> matching the alternate but that’s not right.

See Xavier’s post. My mistake was, essentially, expecting the first "  
to “know” that it was supposed to match a zero-width condition  
governing the state one character later. Instead, of course, it  
asserts itself as a character in its own right; fails to match the  
first alternate; and does match the second.

So [^\s"]+ is indeed probably the best thing. (Other than the  
appropriate real libraries, of course 🙂

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 5:20am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/10 "2006-01-07T05:20:00Z")

</div>

Hi –

On Sat, 7 Jan 2006, K.Kosako wrote:

> Is this pattern work for you?
> 
> str = %Q{abc def “ghi jkl” mno}  
> re3 = /((?\<=“)[^”]+(?=“))|([\S&&[^”]]+)/  
> p str.scan(re3) #=\> [[nil, “abc”], [nil, “def”], [“ghi jkl”, nil], [nil,  
> “mno”]]

Sorry – I tested that accidentally with an old 1.9.0. Yes, with  
today’s I do get the same as you.

(And I also understand why it’s choosing the right-hand alternate 🙂  
See later posts in thread.)

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 5:29am UTC](https://www.ruby-forum.com/t/oniguruma-lookbehind-question/94639/11 "2006-01-07T05:29:00Z")

</div>

On Sat, 07 Jan 2006 04:12:53 -0000, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Argh, I meant ‘if not’. I’m too tired now, I’ve spent too long  
> > perfecting this quiz thing…  
> > I was guessing it was dropping the match without the capture and so  
> > always matching the alternate but that’s not right.
> 
> See Xavier’s post. My mistake was, essentially, expecting the first "  
> to “know” that it was supposed to match a zero-width condition  
> governing the state one character later. Instead, of course, it  
> asserts itself as a character in its own right; fails to match the  
> first alternate; and does match the second.

Oh Damn it, yeah I see now. Wish I’d held my tongue now 😃
