MATLAB Answers

How do I extract the last Time value from a .txt file using regexp?

1 view (last 30 days)
BrainOnLunchBreak
BrainOnLunchBreak on 6 Mar 2018
Commented: Elias Gule on 9 Mar 2018
I have a few hundred files that has header information. An example is as follows:
Measurement: ENERGY
Date: 02/31/2019,02/31/2018
Time: 10:34:51.49,15:21:39.24
Temperature (C): 32.82,8.88,-10.07,32.66,8.94,-10.07
TEC Silicon Temperature (C): 9.90,9.88
Battery Voltage: 7.52,7.41
I want to be able to extract the second time values from each of the files and I have the following extract of code:
filetext = fileread(NameIn);
expr = '(?s)\sTime:\s\d+:\d\d\:\d\d\.+\d+\,(?P<myfield>)+';
time = regexp(filetext, expr, 'once','match')
But, the output is a 0x0 empty char array. What am I doing wrong? :(

Answers (1)

Elias Gule
Elias Gule on 6 Mar 2018
For this particular case, the following 'regex' seems to be working:
expr = '(?<=Time\s*:\s*(\d{2}:\d{2}:\d{2}\.\d{2},\d{2}:\d{2}:))(?<your_field>(.*))';
time = regexp(txt, expr, 'names','dotexceptnewline');
When a match is found this will return a struct array with a field named: "your_field".

Community Treasure Hunt

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

Start Hunting!