Your line-14 regex is going to match anything your line-17 regex would match, and the
next will prevent the following line from running (until the next pass through the loop). When the matches fail, lines 15 and 18 will both execute, and
$1 may not be set.
I think you've created some tangled spaghetti code, here, with your uses of next. I suggest you use if-else blocks.
Caution: Contents may have been coded under pressure.