$subject =~ tr/a-zA-Z0-9_/ /sc; my @words = split(' ', $subject); my $regex = join(' ', map { "($_)?" } @words); my $num_matches = () = $potential_match =~ $regex; if ($num_matches == (@words - 1)) { register($words[-1]); }