代码如下,比如想把 ab+ 的都找出来,比如 gsub 的例子里,都替换成大写的了; 但是怎么从 MatchData 实例里得到第 n(n>1)组匹配结果的信息呢? 类似 php 或者 Java 那样,能取得每组匹配的信息?
类似这个问题:https://www.ruby-forum.com/topic/1435305
irb(main):146:0> a="abbcdefgabbbbcdefg"
=> "abbcdefgabbbbcdefg"
irb(main):147:0> b = a.gsub(/(ab+)/){|y| y.upcase}
=> "ABBcdefgABBBBcdefg"
irb(main):148:0> a="abbcdefgabbbbcdefg"
=> "abbcdefgabbbbcdefg"
irb(main):149:0> x =/(ab+)/.match a
=> #<MatchData "abb" 1:"abb">
irb(main):150:0> x[0]
=> "abb"
irb(main):151:0> x[1]
=> "abb"
irb(main):152:0> x[2]
=> nil
irb(main):153:0> x.length
=> 2
irb(main):154:0> x.offset 1
=> [0, 3]
irb(main):155:0> x.offset 0
=> [0, 3]