extracting the same part from multiple strings

Asked 2 years ago, Updated 2 years ago, 69 views

text_1='hoge'
text_2 = 'hoge kakikakeko'
text_3 = 'hoge'

Expected value = > 'hoge'
text_1='hoge'
text_2 = 'hoge Ayeo Kakikakeko'
text_3='hoge'

Expected value = > 'hoge'

Only the 前方forward match 」

I'd like to do this with ruby, but I don't know what to do at all.
If anyone understands, please let me know.

ruby

2022-09-30 21:17

2 Answers

The library differ is easy to use.

$gem install differ
require 'differ'

def diff(strs)
  strs.reduce do | s1, s2 |
    s=Differ.diff_by_char(s1, s2).to_s
    US>%s[0..s.index('{')-1]
  end
end

list1 = ['hoge Ayeo', 'hoge Kakikakeko', 'hoge Sashiso']
list2 = ['hoge Ayeo', 'hoge Ayeo Kakikakeko', 'hoge Ayeo Sashiso']


puts diff(list1)# hoge
puts diff(list2)# hoge yomiireru

To put it simply, the Differ.diff_by_char() method compares the two strings by character and returns the difference as follows:

require 'differ'

puts Diff.diff_by_char('abc', 'abd').to_s
# ab {"d" > > "c"}

puts Diff.diff_by_char('ab', 'abc').to_s
# ab { - "c" }

Therefore, assuming that the comparison string does not contain {, the string before the difference { appears can be considered a forward match.

additional:

Rewrite as follows to ensure that { is present in the target string.

require 'differ'

def diff(strs)
  strs.reduce do | s1, s2 |
    diff=Differ.diff_by_char(s1,s2)
    raw=diff.send(:raw_array)
    (raw[0].is_a?String)?raw[0]:'
  end
end

list1 = ['hoge{Ayeo', 'hoge{Kakikakeko', 'hoge{Sashiso']
list2 = ['hoge Ayeo', 'hoge Ayeo Kakikakeko', 'hoge Ayeo Sashiso']


puts diff(list1)#hoge{
puts diff(list2)# hoge yomiireru


2022-09-30 21:17

(I'm not used to Ruby, so please think about it for reference only.)

See the comments for processing each line.Also, we omit the first place where strings are stored in an array.

#coding:utf-8
# words = ['hoge Ayeo', 'hoge Kakikakeko', 'hoge Sashiso']
words = ['hoge Ayeo', 'hoge Ayeo Kakikakeko', 'hoge Ayeo Sashiso']
# words = ['a', 'a', 'a']
# words=['a', 'a', 'i']
# words=[', 'a', 'a']
# words = [ ', ', ', ' ]
# words = [ ]
m = words.map { | v | v.length }.min
words.map { | v | v[0,m].chars.to_a}#Match the shortest string length
   .transpose#transpose
   .take_while{|v|v.all?{|i|i==v[0]}}#Take as long as all elements continue to be equal
   .map {|v|v.first}#Each array must have its beginnings removed
   .inject(""){|str,c|str+c}# Summarize into a string
$rubycommonseq.rb
"Hoge, I love you".

The array returned by transpose has empty or non-empty elements, so subsequent operations are safe.


2022-09-30 21:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.