필터 지우기
필터 지우기

how to match two consecutive items

조회 수: 1 (최근 30일)
Mekala balaji
Mekala balaji 2018년 3월 29일
댓글: Mekala balaji 2018년 4월 13일
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에서 Simulink Functions에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by