MATLAB Answers

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

2 ビュー (過去 30 日間)
BrainOnLunchBreak 2018 年 3 月 6 日
コメント済み: Elias Gule 2018 年 3 月 9 日
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? :(

回答 (1 件)

Elias Gule
Elias Gule 2018 年 3 月 6 日
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".
  2 件のコメント
Elias Gule
Elias Gule 2018 年 3 月 9 日
ok. try the attached code and text file.



Community Treasure Hunt

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

Start Hunting!

Translated by