在Matlab

时间:2015-09-30 19:53:01

标签: regex string matlab matching

Matlab中的字符串匹配问题。 如果我有一个矩阵

a = ['thehe'];
str = {'the','he'};
match = regexp(a,str);

输出匹配=

[1]    [1x2 double]

因为它发现'他'两次'和'一次' 我怎么能这样做它从我的字符串的左边到右边看a和 只匹配'一次'和'他'一次?

2 个答案:

答案 0 :(得分:2)

要回答明确的问题,可以从the documentation for regexp指定once搜索选项:

a = 'thehe';
str = {'the','he'};
match = regexp(a,str, 'once');

返回:

match = 

    [1]    [2]

其中match1x2 cell数组,其单元格值对应a中每个str单元格的匹配的第一个索引。

答案 1 :(得分:0)

我从我读过的模糊描述的细节中了解到,您希望thehe的非交错出现的索引,意味着1和4。

a = ['thehe'];
str = {'the';'[^t]he'};
match = regexp(a,str)

打印完两个结果后。

a(match{1}:match{1}+2)

ans =

the

a(match{2}+1:match{2}+2)

ans =

he

没有第三次出现!

a(match{3})

??? Index exceeds matrix dimensions.