Remove text after a symbol
110 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
alejandro paradiso
le 16 Nov 2022
Commenté : Les Beckham
le 16 Nov 2022
Good day to everyone, i'm stuck with separate/erase some words from a sentence.
I'm filtering some data from a table and I don't want to keep anything after the space before hyphen.
Column is already a string
'Bohemian Rhapsody - Remastered 2011'
'(I Can''t Get No) Satisfaction - Mono Version'
'My Generation - Stereo Version'
'Hotel California - 2013 Remaster'
'Born to Run'
'Tears in Heaven - Acoustic; Live at MTV Unplugged, Bray Film Studios, Windsor, England, UK, 1/16/1992; 2013 Remaster'
I've tried with extractAfter but if the name does not contain hyphen, like Born to Run, the outcome is zero or missing. I've also created a pattern with wildcardPattern but, as you can the variations are too much
thanks everyone who will answer
0 commentaires
Réponse acceptée
Les Beckham
le 16 Nov 2022
If the titles are character vectors in a cell array:
titles = ...
{ 'Bohemian Rhapsody - Remastered 2011'
'(I Can''t Get No) Satisfaction - Mono Version'
'My Generation - Stereo Version'
'Hotel California - 2013 Remaster'
'Born to Run'
'Tears in Heaven - Acoustic; Live at MTV Unplugged, Bray Film Studios, Windsor, England, UK, 1/16/1992; 2013 Remaster' };
short_titles = titles; % copy the original titles
idx = contains(short_titles, ' -'); % find the ones that contain ' -'
short_titles(idx) = extractBefore(short_titles(idx), ' -') % remove the unwanted parts from those
The same code also works if they are in a string array:
titles = ...
[ "Bohemian Rhapsody - Remastered 2011"
"(I Can't Get No) Satisfaction - Mono Version"
"My Generation - Stereo Version"
"Hotel California - 2013 Remaster"
"Born to Run"
"Tears in Heaven - Acoustic; Live at MTV Unplugged, Bray Film Studios, Windsor, England, UK, 1/16/1992; 2013 Remaster" ];
short_titles = titles; % copy the original titles
idx = contains(short_titles, ' -'); % find the ones that contain ' -'
short_titles(idx) = extractBefore(short_titles(idx), ' -') % remove the unwanted parts from those
2 commentaires
Les Beckham
le 16 Nov 2022
You are quite welcome.
The result will be empty (0x0 char), not actually zero, if you try to use extractBefore and the string doesn't contain what you are looking for. That is why we need the extra step of finding the ones that definitely contain the search pattern and then only using extractBefore on them.
titles = ...
{ 'Bohemian Rhapsody - Remastered 2011'
'(I Can''t Get No) Satisfaction - Mono Version'
'My Generation - Stereo Version'
'Hotel California - 2013 Remaster'
'Born to Run'
'Tears in Heaven - Acoustic; Live at MTV Unplugged, Bray Film Studios, Windsor, England, UK, 1/16/1992; 2013 Remaster' };
extractBefore(titles, ' -')
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Measurements and Spatial Audio dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!