Extracting second number after comma within parenthesis

17 次查看(过去 30 天)
I have a string
"Toc(Clock Data Ref Time) : 0x91E6 (37350,5.976000e+005 s)";
I am looking to extract only contents after comma from the second parenthesis.
So, the required would be 5.976000e+005.
My code is
XX="Toc(Clock Data Ref Time) : 0x91E6 (37350,5.976000e+005 s)";
TOC=strrep(XX,'Toc(Clock Data Ref Time)','');
TOC=regexp(TOC, '(?<=\()[^)]*(?=\))', 'match')
Which returns 37350,5.976000e+005 s.
But how to extract numbers after comma?
Thank you.

采纳的回答

Allen
Allen 2020-1-20
Assuming that the number will always be in scientific notation, then the following should work.
TOC = regexp(str,'(?<=\(.*?,)\d\.\d+e\+\d+(?=.*?\))','match');
% A rough description of what pattern the expression is indicating
% (?<=\(.*?,) Look behind an open parenthesis for an optional set of any characters until reaching the following pattern
% \d\.\d+e\+\d+ A single digit followed by a period, followed by 1 or more consecutive digits, followed by e+,
% followed by 1 or more consecutive digits.
% This pattern this must precede the following look-ahead assertion.
% (?=.*?\)) Any optional set of characters before a closed parenthesis.
  5 个评论
Stephen23
Stephen23 2020-1-21
编辑:Stephen23 2020-1-21
The regexp documentation is more focussed on the function itself, for detailed documentation on regular expressions in MATLAB read this (and the links at the bottom of that page):

请先登录,再进行评论。

更多回答(3 个)

dpb
dpb 2020-1-20
Slightly modified version following IA's direction using newer string parsing functions that can do much of what regexp expressions are often used for--
XX = "Toc(Clock Data Ref Time) : 0x91E6 (37350,5.976000e+005 s)";
TOC=str2double(extractBetween(XX,',',' '));
>> TOC
TOC =
597600
>>
  2 个评论
Image Analyst
Image Analyst 2020-1-20
Thanks for letting us know about that function. +1 vote. I'd never heard of it.
Seems like they really beefed up and simplified the string handling with extractBetween(), endsWith(), startsWith(), etc. About time. regexp() is just too complicated for most people.
dpb
dpb 2020-1-20
编辑:dpb 2020-1-20
I discovered them when exploring the new strings class back when it was introduced.
They're not that easy to find on their own, however, the "See Also" links don't include them in many logical places like under any of the historical strfind strcmp routines nor even with string itself. They're listed under a topic "Search and Replace Text" but it's a long and arduous road to even get to that link from top level.
I've made the suggestion documentation needs more links to help make them visible but so far hasn't made it to the top of the list (which I reckon must be miles long)...

请先登录,再进行评论。


Image Analyst
Image Analyst 2020-1-20
If your format is fixed (the same every time), you can do it much, much more simply, and less cryptically, by avoiding regexp() and simply using indexing:
XX = "Toc(Clock Data Ref Time) : 0x91E6 (37350,5.976000e+005 s)";
% Convert from string to character array.
XX = char(XX);
% Extract known, fixed part of string from between 47 and 59, inclusive.
TOC = XX(47:59) % This is a character array. Use str2double() if you want a number.
  4 个评论
SATINATH DEBNATH
SATINATH DEBNATH 2020-1-21
Small doubt :). Its working for this particular string.. My data is an array so why it is not working over fulllength of the array unlike using regexp.
Image Analyst
Image Analyst 2020-1-21
You gave XX as a string class variable, not as a character array, so that's why I had to use XX=char(XX). If your data is already a character array just delete that line because (for some reason) indexing doesn't seem to work with strings. Otherwise, attach XX in a .mat file if you're still interested in pursuing the strfind() method.
save('answers.mat', 'XX');

请先登录,再进行评论。


Stephen23
Stephen23 2020-1-20
Simply match all text from the comma to the whitespace:
>> str = 'Toc(Clock Data Ref Time) : 0x91E6 (37350,5.976000e+005 s)';
>> regexp(str,'(?<=,)\S+','match')
ans =
'5.976000e+005'
  3 个评论
SATINATH DEBNATH
SATINATH DEBNATH 2020-1-21
I am using matlab 2014. So extractBetween is not supported there.
dpb
dpb 2020-1-21
编辑:dpb 2020-1-21
Then the substring selection between str(i1:i2) will. Of course, one can roll your own version of extractBetween that way as well...altho regexp() then may well be a better choice.

请先登录,再进行评论。

类别

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