#!/usr/bin/env perl use warnings; use strict; my $string = 'hello'; my $allowed = 'a-zA-Z_-'; if ( $string =~ /^([+$allowed]+)\z/ ) { say $1; }