Find and remove part of a string which precedes a particular substring
8 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Steven Lord
on 16 Dec 2021
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 Comments
Walter Roberson
on 16 Dec 2021
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.
More Answers (1)
Walter Roberson
on 16 Dec 2021
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'))
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!