mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* re.c (match_backref_number): new function for converting a backref
name/number to an integer. (match_offset): use match_backref_number. (match_begin): ditto. (match_end): ditto. (name_to_backref_number): raise IndexError instead of RuntimeError. (match_inspect): show capture index. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a44f38ff52
commit
18d8fbac54
3 changed files with 110 additions and 6 deletions
|
@ -49,4 +49,27 @@ class TestRegexp < Test::Unit::TestCase
|
|||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
def test_named_capture
|
||||
m = /&(?<foo>.*?);/.match("aaa & yyy")
|
||||
assert_equal("amp", m["foo"])
|
||||
assert_equal("amp", m[:foo])
|
||||
assert_equal(5, m.begin(:foo))
|
||||
assert_equal(8, m.end(:foo))
|
||||
assert_equal([5,8], m.offset(:foo))
|
||||
#assert_equal(["amp"], m.values_at(:foo))
|
||||
|
||||
assert_equal("aaa [amp] yyy", "aaa & yyy".sub(/&(?<foo>.*?);/, '[\k<foo>]'))
|
||||
|
||||
assert_equal('#<MatchData "& y" foo:"amp">',
|
||||
/&(?<foo>.*?); (y)/.match("aaa & yyy").inspect)
|
||||
assert_equal('#<MatchData "& y" 1:"amp" 2:"y">',
|
||||
/&(.*?); (y)/.match("aaa & yyy").inspect)
|
||||
assert_equal('#<MatchData "& y" foo:"amp" bar:"y">',
|
||||
/&(?<foo>.*?); (?<bar>y)/.match("aaa & yyy").inspect)
|
||||
assert_equal('#<MatchData "& y" foo:"amp" foo:"y">',
|
||||
/&(?<foo>.*?); (?<foo>y)/.match("aaa & yyy").inspect)
|
||||
|
||||
# MatchData#keys
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue