diff options
author | Nobuyoshi Nakada <[email protected]> | 2021-09-16 19:37:52 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2021-09-16 19:55:06 +0900 |
commit | 09d724e6f846b4e53e8571d41ca7d3055d732d9f (patch) | |
tree | d1438b730b4201f4a02cd67a1e3d1e6a73b78c6f /test/ruby/test_regexp.rb | |
parent | ddb32e66160ab50849419ef7c7ac584913b79c34 (diff) |
[Feature #18172] Add MatchData#match
The method to return the single matched substring corresponding to
the given argument.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/4851
Diffstat (limited to 'test/ruby/test_regexp.rb')
-rw-r--r-- | test/ruby/test_regexp.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index af62d944dc..66a60b8d06 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -473,6 +473,23 @@ class TestRegexp < Test::Unit::TestCase assert_equal("foobarbaz", m.string) end + def test_match_matchsubstring + m = /(.)(.)(\d+)(\d)(\w)?/.match("THX1138.") + assert_equal("HX1138", m.match(0)) + assert_equal("8", m.match(4)) + assert_nil(m.match(5)) + + m = /\A\u3042(.)(.)?(.)\z/.match("\u3042\u3043\u3044") + assert_equal("\u3043", m.match(1)) + assert_nil(m.match(2)) + assert_equal("\u3044", m.match(3)) + + m = /(?<foo>.)(?<n>[^aeiou])?(?<bar>.+)/.match("hoge\u3042") + assert_equal("h", m.match(:foo)) + assert_nil(m.match(:n)) + assert_equal("oge\u3042", m.match(:bar)) + end + def test_match_inspect m = /(...)(...)(...)(...)?/.match("foobarbaz") assert_equal('#<MatchData "foobarbaz" 1:"foo" 2:"bar" 3:"baz" 4:nil>', m.inspect) |