Or simply:
$regex($5,/^\(~?[a-z]\d{3,4}(?:@.*)?\)$/)
The use of parentheses (in place of their octal escapes) accomplishes the same thing, and causes the comma to be treated as part of the string, not as a parameter separator for $regex
To avoid a comma in this case, using \d{3}\d? is another option