Referral groups (parentheses) in a regular expression are executed with `$1 $2 $3`

etc. 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:

```
regexprep("abc","(a)(b)?(c)","$3,$2,$1")
ans = c,b,a
regexprep("ac","(a)(b)?(c)","$3,$2,$1")
ans = ,c,a
```

expected result of the second variant:

```
c,,a
```

here is something similar in `sed`

:

```
echo -e "abcnac" | sed -E 's/(a)(b)?(c)/3,2,1/'
c,b,a
c,,a
```

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:

```
regexprep("ac","(a)(b?)(c)","$3,$2,$1")
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:

```
(w+(((()?d*()))?))
```

should fit `lambda`

and `lambda(1)`

but also catches `lamda()`

versus

```
(w+(((()d+())))?)
```

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.