Remove text after a symbol

106 次查看(过去 30 天)
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

采纳的回答

Les Beckham
Les Beckham 2022-11-16
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
short_titles = 6×1 cell array
{'Bohemian Rhapsody' } {'(I Can't Get No) Satisfaction'} {'My Generation' } {'Hotel California' } {'Born to Run' } {'Tears in Heaven' }
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
short_titles = 6×1 string array
"Bohemian Rhapsody" "(I Can't Get No) Satisfaction" "My Generation" "Hotel California" "Born to Run" "Tears in Heaven"
  2 个评论
alejandro paradiso
alejandro paradiso 2022-11-16
thank you Les, one additional question. I've tried with extractBefore, but the outcome was zero if the song name didn't contain hyphen.
why with indexed extractBefore(short_titles(idx), ' -') is working as I thought?
Les Beckham
Les Beckham 2022-11-16
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, ' -')
ans = 6×1 cell array
{'Bohemian Rhapsody' } {'(I Can't Get No) Satisfaction'} {'My Generation' } {'Hotel California' } {0×0 char } {'Tears in Heaven' }

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Measurements and Spatial Audio 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by