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
댓글 수: 0
채택된 답변
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
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
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, ' -')
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Measurements and Spatial Audio에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!