Find and remove part of a string which precedes a particular substring

8 views (last 30 days)
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 ?

Accepted Answer

Steven Lord
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
result = 3×1 string array
"politics of inclusion" "politics of inclusion are particularly striking in highlighting" "politics of inclusion are being taken up"
result2 = eraseBetween(string(A), 1, P)
result2 = 3×1 string array
"politics of inclusion" "politics of inclusion are particularly striking in highlighting" "politics of inclusion are being taken up"
  2 Comments
Walter Roberson
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
result = 4×1 string array
"politics of inclusion" "politics of inclusion are particularly striking in highlighting" "politics of inclusion are being taken up" <missing>
result2 = eraseBetween(string(A), 1, P)
result2 = 4×1 string array
"politics of inclusion" "politics of inclusion are particularly striking in highlighting" "politics of inclusion are being taken up" "grew three sizes that day!"
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.
Sim
Sim on 16 Dec 2021
Edited: Sim on 16 Dec 2021
Many thanks @Steven Lord,
Many thanks @Walter Roberson,
Both your solutions are great to me!
If possible, I would accept both answers!

Sign in to comment.

More Answers (1)

Walter Roberson
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'))
ans = 3×1 cell array
{'politics of inclusion' } {'politics of inclusion are particularly striking in highlighting'} {'politics of inclusion are being taken up' }

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!