how to match two consecutive items

4 次查看(过去 30 天)
Hi,
I have below data:
Start
PAJ04
Type
Model
End
time
2018-01-2
Data aquire
Start
time
-
Date
2018-02-10
Start time
00:12:24
End
time
00:32:15
Acquired
time
01:15:26
Running sequence
AT09K00
I want catch Date, Start time, End time, acquired time, and running sequence
but some time end time occurs multiple time. I want to catch the first occurrence after the start time
Desired output
Date Start time End time Acquired time Running sequence
2018-02-10 00:12:24 00:32:15 01:15:26 AT09K00
alos How to string match two cosequtive rows for example: to catch "start time:
match first line is "Start", immediate row to be matched is "time", then it is Start time

回答(1 个)

Elias Gule
Elias Gule 2018-3-29
Assuming that you have stored your text in a variable named "txt". The following code should do what you want.
txt = regexprep(txt,'\r?\n',' '); %%replace carriage return and newline character with whitespace
pattern = {'(Date\s*\d{4}-\d{2}-\d{2})',...
'(Start\s*time\s*(\d{2}:\d{2}:\d{2}))',...
'(End\s*time\s*(\d{2}:\d{2}:\d{2}))',...
'(Acquired\s*time\s*(\d{2}:\d{2}:\d{2}))',...
'(Running\s*sequence\s*\w+)'};
output = regexpi(txt,pattern,'match');
output = cellfun(@(x) regexp(x,'\s(?=\d)|(?<=sequence)\s+','split'),...
output,'uni',0);
headerline = char(join(cellfun(@(x) sprintf('%-20s',x{1}{1}),output,'uni',0)));
values = char(join(cellfun(@(x) sprintf('%-20s',x{1}{2}),output,'uni',0)));
output_txt = sprintf('%s\n%s',strtrim(headerline),strtrim(values))
  1 个评论
Mekala balaji
Mekala balaji 2018-4-13
Error using regexpi Multiple strings and patterns given must have the same quantity.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by