Remove text after a symbol

조회 수: 106 (최근 30일)
alejandro paradiso
alejandro paradiso 2022년 11월 16일
댓글: Les Beckham 2022년 11월 16일
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