in reply to Re: Matching multiple patterns with regex
in thread Matching multiple patterns with regex

my @fields = qw/From: Subject:/; # you can add more if/when you want my $field_regex = join( "|", @fields );

I would change the second line to use quotemeta:

my @fields = qw/From: Subject:/; # you can add more if/when you want my $field_regex = join( "|", map { quotemeta($_) } @fields );

It may be unneeded for From: and Subject:, but code may change over time, and adding quotemeta now prevents future bugs.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^3: Matching multiple patterns with regex
by graff (Chancellor) on Oct 31, 2015 at 15:38 UTC
    Actually, I wouldn't recommend quotemeta in tasks like this. It's seldom or never the case that regex-magic characters will be needed as literals in the patterns being conjoined, but even if this comes up, it can still be better to escape them explicitly as needed (when assigning to the array), and allow some strings to use regex-magic where appropriate:
    my @fields = ('From: ', 'Subject: ', 'Thread-\w+: ', 'What-if-there\'s +-a-qmark\?'); # 3rd element matches "Thread-Topic: ", "Thread-Index: ", etc.
    Obviously, in a context where strings are coming from a potentially tainted source (i.e. not from the source code itself), one must weigh the relative risk/benefit and coding-effort/ease-of-use trade-offs of prohibiting vs. allowing (and taint-checking) regex metacharacters in applications like this.