Searching for slightly different versions of one string in another string array

조회 수: 1 (최근 30일)
I am looking for slightly diffeerent versions of a string S in the array A. For example, the string S that I am searching for may look like this:
S='ABC12'
but A may contain various versions of it with various numbers of spaces inbetween the characters:
A={'A BC1 2 ' 'XY T67' 'AB C 12' 'RS U3 2' 'HIJ87' ' A B C1 2'}
Here elements 1, 3 & 6 should all come out as matches, no matter how many spaces between or before/after the characters.

채택된 답변

dpb
dpb 2022년 5월 14일
>> S='ABC12';
>> A(matches(strrep(A,' ',''),S))
ans =
1×3 cell array
{'A BC1 2 '} {'AB C 12'} {' A B C1 2'}
>>
You can get more exotic with regexp or with the newfangled pattern, but for the problem as described, I'd go for simple-minded.
  댓글 수: 3
Saeid
Saeid 2022년 5월 14일
By the way, what if A looks like this:
A={'A BC1 2 STRING1' 'XY T67STRING2' 'AB C 12STRING3' 'RS U3 2STRING4' 'HIJ87STRING5' ' A B C1 2STRING6'}
and I want to isolate the S part and delete the rest (or vice versa)?
dpb
dpb 2022년 5월 15일
That depends on just what "STRINGn" contains in the real data set. With a fixed pattern as given, only a minor modification to the above will still work...
>> A={'A BC1 2 STRING1' 'XY T67STRING2' 'AB C 12STRING3' 'RS U3 2STRING4' 'HIJ87STRING5' ' A B C1 2STRING6'};
>> strtrim(extractBefore(A(startsWith(strrep(A,' ',''),S)),'STRING'))
ans =
1×3 cell array
{'A BC1 2'} {'AB C 12'} {'A B C1 2'}
>>
The assumption of a constant pattern probably won't stand up, so you're probably into a regular expression or using pattern.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by