段落内的strfind split关键字

时间:2014-03-18 08:41:47

标签: matlab matlab-figure

在URL中输出关键字后,如何检查关键字是否存在于页面内容中,如下面的内容,如果是,则返回1,否则返回0.有strfind在那里,但我不知道为什么它不能工作

str = 'http://en.wikipedia.org/wiki/hostname'
Paragraph = 'hostname From wikipedia, the free encyclopedia Jump to: navigation, search In    computer networking, a hostname (archaically nodename .....'
SplitStrings = regexp(str,'[/.]','split')

for it = SplitStrings
c( it{1} ) = strfind(Paragraph, it{1} )
end

SplitStrings = {};

feature11=(cellfun(@(n) isempty(n), strfind(Paragraph, SplitStrings{1})))

enter image description here

我可以使用以下代码4来检查' https'存在与否。但是,如何修改' SplitString'进入' B6'?

str = 'https://en.wikipedia.org/wiki/hostname'

A6 = regexp(str,'\w*://','match','once')
B6 = {'https'};

feature6=(cellfun(@(n) isempty(n), strfind(A6, B6{1})))

2 个答案:

答案 0 :(得分:1)

我绝对不清楚你想在这做什么......

我怀疑是这样的:

str      = 'http://en.wikipedia.org/wiki/hostname';

haystack = 'hostname From wikipedia, the free encyclopedia Jump to: navigation, search In    computer networking, a hostname (archaically nodename .....';
needles  = regexp(str,'[:/.]*','split') %// note the different search string

%// What I think you want to do
~cellfun('isempty', regexpi(haystack, needles, 'once'))

结果:

needles = 
    'http'    'en'    'wikipedia'    'org'    'wiki'    'hostname'
ans =
     0     1     1     0     1     1

但如果不是这种情况,请编辑您的问题,并为一些示例输入添加所需的输出。

修改

好的,所以如果我现在理解你,你想要整个单词而不是部分匹配。您必须通过以下方式告诉regexp

%// NOTE: these  metacharacters indicate that match is to occur 
%//       at beginning AND end of word (so whole words only)
needles  = strcat('\<', regexpi(str,'[:/.]*','split'), '\>') 

%// Search for these words in the paragraph
~cellfun('isempty', regexpi(haystack, needles, 'once'))

答案 1 :(得分:1)

你可以试试这个

f=@(str) isempty(strfind(Paragraph,str))
cellfun(f,SplitStrings)

这应该得到完整的话。关键是解析变量Paragraph以获取它们

SplitParagraph=regexp(Paragraph,'[ ,:.()]','split');
I=ismember(SplitStrings,SplitParagraph);
SplitStrings(I)