I do not completely understand the pattern you posted (it were the doubled ? that surprised me, do they have a special meaning I do not know yet, or are they do indicate "don't know what to do here"?) but I hope I more or less understood what you want.
Is it like: if there is some optional text, the = befor that is required - so only both parts together are optional, but the = itself within this subpattern is not?
that would be:
code(=.*?)?
then, to keep it out of the part reused in the backreference, add additional parentheses:
code(=(.*?))? and increase the number used in the backreference by one.
HTH