开发者

Is there a way to say match 1 or more instances of a defined subsequence/grouping?

开发者 https://www.devze.com 2023-01-10 06:20 出处:网络
require \'rubygems\' require \'dif开发者_高级运维fer\' original = \"Epic lolcat fail bellmead drive!\"
require 'rubygems'  
require 'dif开发者_高级运维fer'

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


I don't know 'differ', but how about:

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]}" }
0

精彩评论

暂无评论...
验证码 换一张
取 消