필터 지우기
필터 지우기

Help with extracting part of a string

조회 수: 30 (최근 30일)
aurc89
aurc89 2014년 9월 26일
편집: Stephen23 2018년 11월 14일
I have the string "2DVIS_data_08_40fs", and I want to extract the numbers included between the last underscore '_' and the last two letters 'fs', in this case 40. Given the string "2DVIS_data_08_120fs", I want as output 120 and so on. How can I do this? thanks

채택된 답변

Chad Greene
Chad Greene 2014년 9월 26일
Guillame's answer is best, and it's probably good to learn how to use the power of regexp. But if you want a more intuitive-for-beginners approach, find the indices of the underscores, find the indices of fs, and then return everything between them:
somestring = '2DVIS_data_08_40fs';
underscore_indices = strfind(somestring,'_');
fs_indices = strfind(somestring,'fs');
yourNumber = str2double(somestring(underscore_indices(end)+1:fs_indices(end)-1))
  댓글 수: 1
aurc89
aurc89 2014년 9월 28일
Thank you, since I'm a beginner this is easier for me

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Stephen23
Stephen23 2014년 9월 26일
편집: Stephen23 2014년 9월 27일
Use regexp .
Given a literal interpretation of your statement "extract the numbers included between the last underscore '_' and the last two letters 'fs'", this could be done using the following regexp match string:
>>A = {'2DVIS_data_08_120fs','2DVIS_data_08_40fs'};
>>B = regexp(A,'(?<=_)\d+(?=fs$)','once','match')
B =
'120' '40'
This match string locates one or more digits located between an '_' and the last two characters of the string (which must be 'fs'). If the strings are always of the same format, then matching of the last two characters would be enough.
  댓글 수: 2
Chad Greene
Chad Greene 2014년 9월 26일
nice. :)
aurc89
aurc89 2014년 9월 28일
Thanks Stephen!

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2014년 9월 26일
Use regexp, you haven't explained the pattern in great details, the following may be what you want:
elems = regexp(s, '_(\d+)_[^_]*(..)$', 'tokens', 'once');
number = str2double(elems{1});
string = elems{2};
It will match any pattern with _, followed by 1 or more digit (an integer), followed by _, followed by any 0 or more characters not including _, followed by two characters at the end of the string. It extracts the integer and the last two characters.
  댓글 수: 2
Guillaume
Guillaume 2014년 9월 27일
Realised I misunderstood the pattern. Stephen's or the following would work:
number = str2double(regexp(s, '_(\d+)..$', 'tokens', 'once'));
If you want to ensure that the last two characters are letters:
number = str2double(regexp(s, '_(\d+)[a-zA-Z]{2}', 'tokens', 'once'));
Or if it's always 'fs':
number = str2double(regexp(s, '_(\d+)fs', 'tokens', 'once'));
Basically, learn regexp.
aurc89
aurc89 2014년 9월 28일
Thank you very much !

댓글을 달려면 로그인하십시오.


Albert Passy
Albert Passy 2018년 11월 14일
"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."
Regexes are expressive, compact and cryptic. reserve them for complex problems.
  댓글 수: 1
Stephen23
Stephen23 2018년 11월 14일
편집: Stephen23 2018년 11월 14일
Hmmm... but if we did not use them for simple problems, then we would not know how to use them properly for complex problems.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by