use warnings; use strict; use feature 'say'; my $string = 'hello'; my $allowed_char = qr/[+a-zA-Z_-]/; if ( $string =~ /^($allowed_char+)\z/ ) { say $1; }