The behavior of 0-length string matches can be special cased in the code, or not. For instance the sed but not perl
$ echo aabb | sed 's/a*/X/g'
$ echo aabb | gsed 's/a*/X/g'
$ echo aabb | ...
You don't say which shell this is being written for, but generally variable names can't start with a digit.
I would strongly recommend that you start to paste your code into https://shellcheck.net (or ...