Given (aba?)+
as the Regex and abab
is the string.
Why does it only matches aba
?
Since the last a in the regex is optional, isn't abab
a match as well?
tested on https://regex101.com/
Given (aba?)+
as the Regex and abab
is the string.
Why does it only matches aba
?
Since the last a in the regex is optional, isn't abab
a match as well?
tested on https://regex101.com/
The reason (aba?)+
only matches aba
out of abab
is greedy matching: The optional a
in the first loop is tested before the group is tested again, and matches. Therefore, the remaining string is b
, which does not match (aba?)
again.
If you want to turn off greedy matching for this optional a
, use a??
, or write your regex differently.
Since (aba?)+ is greedy, your pattern tries to match as much as possible. And since it first matches "aba", the remaining "b" is not matched.
Try the non-greedy version (it will match the first and second "ab"'s):
$ echo "abab" | grep -Po "(aba?)+"
aba
$ echo "abab" | grep -Po "(aba??)+"
abab
The correct regex for this is:
^(aba??)+$
and not (aba??)+
as discussed with @WiktorStribizew and YSelf.