简体   繁体   English

使用 Matlab 查找字符串中包含的 substring

[英]Find which substring is contained in string with Matlab

I have a char variable called "condition".我有一个名为“条件”的 char 变量。 The content of this variable is something like: "21331-54-task-5da1-6256853-35-1-3.mp4".该变量的内容类似于:“21331-54-task-5da1-6256853-35-1-3.mp4”。

I need to determine which one of the following characters is contained in the name: 25, 35, 45. I tried with contains(), but this only tells me whether one of these is contained or not in condition.我需要确定名称中包含以下哪个字符:25、35、45。我尝试使用 contains(),但这仅告诉我其中一个是否包含在条件中。 It doesn't tell me which one is contained.它没有告诉我包含哪一个。

I also tried:我也试过:

strToFind = {'25', '35', '45'}; strToFind = {'25', '35', '45'};

pos = strfind(strToFind, condition) pos = strfind(strToFind,条件)

but it gives me the following result:但它给了我以下结果:

pos =位置 =

1×3 cell array 1×3 单元阵列

{0×0 double} {0×0 double} {0×0 double}

Any suggestion on how to solve this?关于如何解决这个问题的任何建议? Thank you in advance先感谢您

~cellfun(@isempty, regexp(condition, strToFind, 'once'))

will give a logical array the same size as strToFind , telling if each of the strings in strToFind is present in condition .将给出一个与strToFind strToFind的每个字符串是否存在于condition中。

You can also use你也可以使用

cellfun(@(x) contains(condition, x), strToFind)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM