匹配字符串中的多个单词

时间:2014-04-09 09:24:59

标签: matlab

我的查询是string1 = 'Ram is going to school',我要匹配的其他字符串是query_string = Ram school。如何才能将这两个单词与上面的字符串匹配。当且仅当query_string中的两个字都在string1中时,query_stringstring1的匹配才有意义。

string1 = 'Ram is going to school'

query_string = Ram school

output =查询字符串匹配

1 个答案:

答案 0 :(得分:1)

<强>代码

string1 = 'Ram is going to school'
query_string = 'Ram school'

split1 = unique(regexp(string1,'\s','Split'),'stable');
split2 = unique(regexp(query_string,'\s','Split'),'stable');

out = nnz(ismember(split1,split2))==numel(split2)

<强>输出

out =    
     1

修改query_string = 'Ram school days',将out作为0

扩展第1部分:相反,如果您希望获得匹配数量的阈值,请使用此 -

out = nnz(ismember(split1,split2))>=Threshold

扩展第2部分:不区分大小写的匹配

string1 = 'ram is going to schooL';
query_string = 'Ram School';

string1c = unique(regexp(string1,'\s','Split'),'stable');
query_stringc = unique(regexp(query_string,'\s','Split'),'stable');

lstring1c = cellfun(@lower,string1c,'uni',0);
lquery_stringc = cellfun(@lower,query_stringc,'uni',0);

out = nnz(ismember(lstring1c,lquery_stringc))==numel(query_stringc)

<强>输出

out =

     1