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.
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.
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
(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.
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.