Project

General

Profile

Actions

Bug #21859

closed

Inconsistent behaviors in Regexp lookbehind/lookahead with capture groups

Bug #21859: Inconsistent behaviors in Regexp lookbehind/lookahead with capture groups

Added by trinistr (Alexander Bulancov) 21 days ago. Updated 18 days ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.0.1 (2026-01-13 revision e04267a14b) +PRISM [x86_64-linux]
[ruby-core:124664]

Description

First issue: Regexp.linear_time? is false for a positive lookahead with a capture, but true for a positive lookbehind:

irb(main):002> Regexp.linear_time?(/(?=(a))/)
=> false
irb(main):003> Regexp.linear_time?(/(?<=(a))/)
=> true

This should be false in both cases.

Second issue: Capture group is allowed in a negative lookahead, but causes a SyntaxError in a negative lookbehind:

irb(main):001> /(?!(a))b/
=> /(?!(a))b/
irb(main):002> /(?<!(a))b/
/home/alex/.local/share/mise/installs/ruby/4.0.1/lib/ruby/gems/4.0.0/gems/irb-1.16.0/exe/irb:9:in '<top (required)>': (irb):2: invalid pattern in look-behind: /(?<!(a))b/ (SyntaxError)

I believe such a capture group can never capture anything, so it probably should be an error in both cases.

Actions

Also available in: PDF Atom