I am often faced with patterns where the part which is interesting is delimited by a specific character, the rest does not matter. A typical example:
/dev/sda1 472437724 231650856 216764652 52% /
I would like to extract 52
(which can also be 9
, or 100
- so 1 to 3 digits) by saying "match anything, then when you get to %
(which is unique in that line), see before for the matches to extract".
I tried to code this as .*(\d*)%.*
but the group is not matched:
.*
match anything, any number of times%
... until you get to the litteral%
(the\d
is also matched by.*
but my understanding is that once%
is matched, the regex engine will work backwards, since it now has an "anchor" on which to analyze what was before -- please tell if this reasoning is incorrect, thank you)(\d*)
... and now before that%
you had a(\d*)
to match and group.*
... and the rest does not matter (match everything)