\b(?:a(?:bout|bove|cross|fter|fterwards|))\b #### use constant PREFIXSIZE => 1; my @stopwords = qw( a about above across after afterwards ); # presumably much larger, with other first letters... my $curfirst; my %buildhash = (); # create first => rest mapping: for ( @stopwords ) { my $first = substr( $_, PREFIXSIZE, 1, "" ); push @{$buildhash{$first}}, $_; } # use letter hash to build regex my $regex = ''; for ( sort keys %buildhash ) { $regex .= "(?:\\b$_"; if ( @{$buildhash{$_}} > 1 ) { $regex .= "(?:" . join( '|', @{$buildhash{$_}} ) . ")\\b)|"; } else { $regex .= ${$buildhash{$_}}[0] . "\\b)|"; } } # ditch trailing pipe substr( $regex, -1, 1, '' ); print $regex, "\n"; __END__ prints: \b(?:a(?:|bout|bove|cross|fter|fterwards))\b