Should I change hte Perl regular expression to something else?
Your REGEXP isn't strict enough. You'd need to use the below.
/^[a-zA-Z0-9]+$/
Or should I change the authorized input to single a-z,A-Z,0-9,_ word?
That would be the easier approach as all those characters should be valid for Moodle as well.