Get substring from text
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi,
I am new to matlab and I have a problem. I got a conversation between a group of people and I have a list of their names. I need to get a list of their names as they were mentioned in the conversation. The conversation and the names are cell arrays.
For example, the conversation is "Hi B, Hi A, Hey guys D can't come, Too Bad C, I Feel bad for D" and its a 1x5 cell array. The list of names is "A, B, C, D" and its a 1x4 cell array. I need the list "B, A, D, C, D" and its a 1x 5 cell array.
TIA
댓글 수: 0
채택된 답변
Walter Roberson
2020년 4월 6일
S = {'Hi B', 'Hi A', 'Hey guys D can''t come', 'Too Bad C', 'I Feel bad for D'};
names = {'A', 'B', 'C', 'D'};
namepattern = ['\<', strjoin(names,'\\>|\\<'), '\>'];
matched_names = regexp(S, namepattern, 'match');
The result will be a cell array the same size as S, with each entry containing a cell array of character vectors. The contained cell array will be empty if there are no name matches; it will contain a scalar cell array that contains a character vector if there is one match (the case you are expecting), and the contained cell array could contain multiple character vectors if multiple names were matched.
In the particular case where you are sure you only have at most one match, or you only want the first match if there are multiple matches, then you can use
matched_names = regexp(S, namepattern, 'match', 'once');
In that case, instead of matched_names being a cell array containing cell arrays, it would be a cell array directly containing character vectors
This code does not require that the names are single letters, and does not require that they are upper case. It will, however, only recognize them if they are isolated -- for example 'Too Bad C' it will detect that the 'B' in 'Bad' is followed by something that is word-like and so will not treat it as a match for the name 'B'. The code will consider digits and underscore to be word-like, so for example 'B_' and 'B3' would not be considered to match 'B' but 'B.' or 'B?' would be considered to match.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!