Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1(최근 30일)
BrainOnLunchBreak
BrainOnLunchBreak 2018년 3월 6일
마감: MATLAB Answer Bot 2021년 8월 20일
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".

Community Treasure Hunt

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

Start Hunting!

Translated by