in reply to Array within regexp?

Introducing join.

my @titles = qw/ Mr Mrs Ms Miss Dr Sir Lord Lady /; $alt_titles = join '|', @titles; if( $text =~ m/^($alt_titles)\s+(.*)/ ) { print "$1 => $2\n"; }

Dave

Replies are listed 'Best First'.
Re^2: Array within regexp?
by ambrus (Abbot) on Feb 27, 2012 at 11:04 UTC

    Correct. However, if you mean the array to contain plain strings you want to match literally, you need to tell that to perl, otherwise it will try to interpret them as regexen, such as interpretting dots in the strings to match any character.

    $alt_titles = join '|', map quotemeta, @titles;

      Thanks for your good answers - I didn't think that would be so easy!

        If you didn't expect it to be easy, why are you using Perl? ;)

        Perl makes simple things easy and hard things possible.
        True laziness is hard work