Find and remove part of a string which precedes a particular substring
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sim
el 16 de Dic. de 2021
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 comentarios
Respuesta aceptada
Steven Lord
el 16 de Dic. de 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 comentarios
Walter Roberson
el 16 de Dic. de 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.
Más respuestas (1)
Walter Roberson
el 16 de Dic. de 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'))
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!