有没有办法说匹配定义的子序列/分组的1个或多个实例?

时间:2010-08-01 19:50:58

标签: ruby regex

require 'rubygems'  
require 'differ'

original = "Epic lolcat fail bellmead drive!"  
current = "Epic wolfman fail bellmead dr!"

diff = Differ.diff_by_word(current, original).to_s   

puts diff # => "Epic {"lolcat" >> "wolfman"} fail bellmead {"drive" >> "dr"}!

diff =~ /(\{"[a-zA-Z0-9]*" >> "[a-zA-Z0-9s]*"\})/

puts "#{$1} to #{$2} #{$3} to #{$4}"
# => lolcat to wolfman to

# Desired output: lolcat to wolfman to drive to dr

1 个答案:

答案 0 :(得分:1)

我不知道'不同',但如何:

require 'rubygems'  
require 'differ'

original = "Epic lolcat fail bellmead drive!"  
current = "Epic wolfman fail bellmead dr!"

diff = Differ.diff_by_word(current, original).to_s   

puts diff # => "Epic {"lolcat" >> "wolfman"} fail bellmead {"drive" >> "dr"}!

captures = diff.scan /\{"([a-zA-Z0-9]*)" >> "([a-zA-Z0-9s]*)"\}/

captures.each {|c| puts "#{c[0]} to #{c[1]}" }