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

12 visualizaciones (últimos 30 días)
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 ?

Respuesta aceptada

Steven Lord
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
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 comentarios
Walter Roberson
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
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 el 16 de Dic. de 2021
Editada: Sim el 16 de Dic. de 2021
Many thanks @Steven Lord,
Many thanks @Walter Roberson,
Both your solutions are great to me!
If possible, I would accept both answers!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
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'))
ans = 3×1 cell array
{'politics of inclusion' } {'politics of inclusion are particularly striking in highlighting'} {'politics of inclusion are being taken up' }
  1 comentario
Sim
Sim el 16 de Dic. de 2021
Many thanks @Walter Roberson,
Many thanks @Steven Lord,
Both your solutions are great to me!
If possible, I would accept both answers!

Iniciar sesión para comentar.

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!

Translated by