Find string that has multiple substrings
이전 댓글 표시
I have a cell array each cell containing a string and I am trying to find all cells that have contain 2 substrings. For example
A = [Car is fast; Car is slow; Train is fast; Plane is fast]
I am new to cellfun (that doesn't necessarily have to be the solution) but figured that was the only way to do it.
any(~cellfun('isempty',strfind(A,'Car' 'fast')))
The result should be
[1;0;0;0]
채택된 답변
추가 답변 (1개)
Walter Roberson
2016년 9월 28일
A = {'Car is fast'; 'Car is slow'; 'Train is fast'; 'Plane is fast'};
targets = {'Car', 'fast'};
lit_targets = regexptranslate('escape', targets);
pattern = [sprintf('(?=.*%s)', lit_targets{:}) '.?'];
matches = ~cellfun(@isempty, regexp(A, pattern) );
This is extendable to any number of strings in targets.
The step with regexptranslate is to ensure that anything in targets is matched literally. For example if you had 'Car.' then the period needs to be treated as an exact period. Without this step, regexp would treat the period as meaning "any one character"
There is another potential approach:
A = {'Car is fast'; 'Car is slow'; 'Train is fast'; 'Plane is fast'};
targets = {'Car', 'fast'};
lit_targets = regexptranslate('escape', targets);
pattern = strjoin(lit_targets, '|');
matches = cellfun(@length, regexp(A, pattern)) >= length(targets);
However, this will have problems if there is a string that contains multiple copies of one of the words. For example, 'This is a test' contains two copies of 'is' so if you were searching for 'is' and 'car' then the two matches for 'is' would count as 2 and the code would not notice that 'car' was not there. This approach is therefore not recommended for the general purpose.
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!