1
0
Fork 0
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:
akr 2007-12-09 07:12:44 +00:00
parent a44f38ff52
commit 18d8fbac54
3 changed files with 110 additions and 6 deletions

View file

@ -49,4 +49,27 @@ class TestRegexp < Test::Unit::TestCase
:ok
end
end
def test_named_capture
m = /&(?<foo>.*?);/.match("aaa &amp; 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 &amp; yyy".sub(/&(?<foo>.*?);/, '[\k<foo>]'))
assert_equal('#<MatchData "&amp; y" foo:"amp">',
/&(?<foo>.*?); (y)/.match("aaa &amp; yyy").inspect)
assert_equal('#<MatchData "&amp; y" 1:"amp" 2:"y">',
/&(.*?); (y)/.match("aaa &amp; yyy").inspect)
assert_equal('#<MatchData "&amp; y" foo:"amp" bar:"y">',
/&(?<foo>.*?); (?<bar>y)/.match("aaa &amp; yyy").inspect)
assert_equal('#<MatchData "&amp; y" foo:"amp" foo:"y">',
/&(?<foo>.*?); (?<foo>y)/.match("aaa &amp; yyy").inspect)
# MatchData#keys
end
end