Matching two texts
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have two texts . is it possible to extract the text where the two arrays match.For example: A='First Boston Corp Lehman Brothers ' B='Lehman Brothers Merill Lynch'; How can I get the match "Lehman Brothers"
采纳的回答
Teja Muppirala
2011-6-29
The simple brute force method:
A='Lehman Brothers Merill Lynch';
B='First Boston Corp Lehman Brothers';
for n = 1:numel(B);
for k = 1:n
if ~isempty(strfind(A,B(k + (0:numel(B)-n))))
Bmatch = B(k + (0:numel(B)-n))
return
end
end
end
0 个评论
更多回答(2 个)
Matt Fig
2011-6-29
A = 'First Boston Corp Lehman Brothers ';
B = 'Lehman Brothers Merill Lynch';
Am = regexp(A,'\s','split');
Am = Am(ismember(Am,regexp(B,'\s','split')))
1 个评论
Walter Roberson
2011-6-29
That finds words in common, not substrings in common. For example if B='Brothers Merill Lehman Lynch' then that algorithm would output {'Lehman' 'Brothers'} even though 'Brothers ' is the longest common substring.
Longest substring could potentially be 'Lehman Brother' if one of the strings had 'Lehman Brothers' and the other had 'Lehman Brotherhood'. It is not completely clear from Joseph's description whether only "words" are to be matched or whether parts of words are okay as well.
Walter Roberson
2011-6-29
This is the "longest common substring problem"; see http://en.wikipedia.org/wiki/Longest_common_substring_problem (which looks a bit biased in that it only presents one algorithm)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!