Comparing matching patterns with capture using MATLAB regex

I am trying to parse a log file that looks like this:

%%%% 09-May-2009 04:10:29
% Starting foo
this is stuff
to ignore
%%%% 09-May-2009 04:10:50
% Starting bar
more stuff
to ignore
%%%% 09-May-2009 04:11:29
...

This excerpt contains two time periods that I would like to extract: from the first separator to the second and from the second to the third. I would like to use regex to extract the start and stop times for each of these intervals. This basically works:

p = '%{4} (?<start>.*?)\n% Starting (?<name>.*?)\n.*?%{4} (?<stop>.*?)\n';
times = regexp(c,p,'names');

Return:

times = 

1x16 struct array with fields:
    start
    name
    stop

The problem is that it only captures every other period, since the second delimiter is consumed as part of the first match.

(lookahead, lookbehind) . , MATLAB, , . , , ( ).

?

P.S. , , -, .

: , . , MATLAB.

+3
3

MATLAB , (, , MATLAB REGEXP). , , , REGEXP, , . :

c =

%%%% 09-May-2009 04:10:29
% Starting foo
this is stuff
to ignore
%%%% 09-May-2009 04:10:50
% Starting bar
more stuff
to ignore
%%%% 09-May-2009 04:11:29
some more junk

... :

p = '%{4} (?<start>[^\n]*)\n% Starting (?<name>[^\n]*)[^%]*|%{4} (?<start>[^\n]*).*';

:

names = regexp(c,p,'names');
[names.stop] = deal(names(2:end).start,[]);
names = names(1:end-1);

..., :

>> names(1)

ans = 

    start: '09-May-2009 04:10:29'
     name: 'foo'
     stop: '09-May-2009 04:10:50'

>> names(2)

ans = 

    start: '09-May-2009 04:10:50'
     name: 'bar'
     stop: '09-May-2009 04:11:29'
+2

, Perl Matlab. Perl .

+1

All you have to do is wrap the look around the part of the regex matching the second timestamp:

'%{4} (?<start>.*?)\n% Starting (?<name>.*?)\n.*?(?=%{4} (?<stop>.*?)\n)'

EDIT: here it has no named groups:

'%{4} (.*?)\n% Starting (.*?)\n.*?(?=%{4} (.*?)\n)'
0
source

Source: https://habr.com/ru/post/1708094/


All Articles