Skip to content

#named_captures with regexp union does not match regexp.match behavior #145

@yxhuvud

Description

@yxhuvud

Given the following:

require "strscan"
scanner = StringScanner.new('foobar')

re1 = /(?<foo>foo)/
re2 = /(?<foo>bar)/

re = Regexp.union([re1, re2])
m = scanner.scan(re)
p m
p scanner.captures
p scanner.named_captures
p 'foobar'.match(re).named_captures

It will output:

foo"
["foo", nil]
{"foo" => nil}
{"foo" => "foo"}

Removing re2 from the union will have the scanner named captures as {"foo" => "foo"} as expected.

edit: Using re = /#{re1}|#{re2}/ also seems to have this behavior.

ruby version: ruby 3.4.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions