use Regexp::Assemble; my $r = Regexp::Assemble->new; while( ) { chomp; $r->add( $_ ); } print $r->as_string; # produces ^(?:K[LM]|P[AM]|S[LZ]|CP|ME|WX|YZ)XX1 __DATA_ ^CPXX1 ^KLXX1 ^KMXX1 ^MEXX1 ^PAXX1 ^PMXX1 ^SLXX1 ^SZXX1 ^WXXX1 ^YZXX1 #### print Regexp::Assemble ->new( chomp=> 1 ) ->add( ) ->as_string;