#Note that even with single quotes, '\\' is #interpolated to '\'; this doesn't break the regex, #as both sides of =~ will see only '\'. $regex = '^[24\\\\\[\]\{\}\^\$]{24}$'; $text = '2' x 24; # will also match, for example.