python - Interpret regular expression -
this question has answer here:
i'm trying parse output looks below.
1 192.168.1.1 0.706 ms 0.654 ms 0.697 ms 2 10.10.10.10 4.215 ms 4.199 ms 4.175 ms 3 123.123.123.123 4.000 ms * *
i have regular expression works, follows.
this regex works:
re.compile(r'^\s*(\d+)\s+?([\s\s]+?(?=^\s*\d+\s+))', re.m)
this capture each line properly.
('1', ' 192.168.7.1 0.706 ms 0.654 ms 0.697 ms\n') ('2', ' 10.10.10.10 4.215 ms 4.199 ms 4.175 ms\n') ('3', ' 123.123.123.123 4.000 ms * *\n')
my question bold ? in front of (?=^\s*\d+\s+)
i.e. changing regular expression follows.
this regex not work. difference ? mark removed.
re.compile(r'^\s*(\d+)\s+?([\s\s]+(?=^\s*\d+\s+))', re.m)
i have tried , not work. not parse each line separately.
could me interpret regular expression?
according re docs:
*?
,+?
,??
the
'\*'
,'+'
, ,'?'
qualifiers greedy; match text possible. behaviour isn’t desired; if re<.*>
matched against<a> b <c>
, match entire string, , not<a>
. adding?
after qualifier makes perform match in non-greedy or minimal fashion; few characters possible matched. using re<.*?>
match<a>
.
so in case, [\s\s]+?
match space or non-space character minimally, whereas [\s\s]+
greedily match many characters can.
Comments
Post a Comment