Find and remove part of a string which precedes a particular substring
조회 수: 13 (최근 30일)
이전 댓글 표시
Hi, I have cell arrays similar to A
A = {{'all the governments have tended to practice politics of inclusion'}
{'politics of inclusion are particularly striking in highlighting'}
{'the ways in which the politics of inclusion are being taken up'}
...
};
and all of them contain the following substring
'politics of inclusion'
For each of my cell arrays, I would like to detect if there is some text before the substring "politics of inclusion".
If Yes, I would like to remove the text which precedes my substring of interest, i.e. "politics of inclusion", and getting the following output:
B = {{'politics of inclusion'}
{'politics of inclusion are particularly striking in highlighting'}
{'politics of inclusion are being taken up'}
...
};
Any idea on how (i) to detect if some text exists before a certain substring and (ii) how to remove it ?
댓글 수: 0
채택된 답변
Steven Lord
2021년 12월 16일
Convert A to a string array then use the string processing functions like extractAfter and eraseBetween.
A = {{'all the governments have tended to practice a politics of inclusion'}
{'politics of inclusion are particularly striking in highlighting'}
{'the ways in which the politics of inclusion are being taken up'}
};
P = 'politics of inclusion';
result = P + extractAfter(string(A), P) % or
result2 = eraseBetween(string(A), 1, P)
댓글 수: 2
Walter Roberson
2021년 12월 16일
A = {{'all the governments have tended to practice a politics of inclusion'}
{'politics of inclusion are particularly striking in highlighting'}
{'the ways in which the politics of inclusion are being taken up'}
{'grew three sizes that day!'}
};
P = 'politics of inclusion';
result = P + extractAfter(string(A), P) % or
result2 = eraseBetween(string(A), 1, P)
The <missing> result is arguably wrong: if there is no pattern match then there should be no erasing going on.
The eraseBetween() works well though.
추가 답변 (1개)
Walter Roberson
2021년 12월 16일
A = {{'all the governments have tended to practice politics of inclusion'}
{'politics of inclusion are particularly striking in highlighting'}
{'the ways in which the politics of inclusion are being taken up'}
...
};
As = vertcat(A{:});
extractAfter(As, lookAheadBoundary('politics of inclusion'))
참고 항목
카테고리
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!