Referral groups (parentheses) in a regular expression are executed with
$1 $2 $3etc. in GNU Octave and matlab,
But in both GNU Octave and matlab Optional groups are not numbered if they do not match. Is this a bug?
Here is an example:
ans = c,b,a
ans = ,c,a
expected result of the second variant:
here is something similar in
echo -e "abcnac" | sed -E 's/(a)(b)?(c)/3,2,1/'
If I look at the regular expression, there are three sets of parentheses, and the third is definitely the one that should match c so that c should come first in the output. However, since the second does not match, c is updated in the backreferences on slot 2.
I think that this is a mistake because the author of the regular expression may not know if a string will contain the optional bit or not. Regardless, the author must decide in advance what to replace by what reference.
It seems to me that sed If you do it correctly, the numbering should match the parentheses in the regular expression, not the way the groups match.
A workaround I'm using now is to be used
* Quantifier and
? within the group, instead of making groups optional.
In order to:
ans = c,,a
However, this is more complicated with more complex regular expressions, e.g. For example, when searching for a variable with possibly an index:
lambda(1)but also catches
That's what I really want, but that'll mess up the referencing.
I tested it very fast in Matlab and it seemed to be the same.