I recently realized that I have a lot of regexes that look like this:
if (
$string1 =~ /this|that|the other/ ||
$string2 =~ /this|that|the other/
) { $example = 20170107034814; }
Except that the pattern can often contain 50+ different potential matches, which means that I have two long lines that are almost duplicates... which means that there's twice the chance of an error in the coding.
Is there a way that I can make that shorter, easier to read, and less open to errors? I know I could do this:
$pattern = 'this|that|the other';
if (
$string1 =~ /$pattern/ ||
$string2 =~ /$pattern/
) { $example = 20170107034814; }
but I have a lot of elsif() statements, too, so that really just makes it more complicated than I'd want. I'm hoping that maybe there's a shortcut that I don't know, like this:
# This is just made up for an example, don't use it
if (
$string1
|| $string2 =~ /this|that|the other/
) { $example = 20170107034814; }
I'm using Perl version 5.10, so I'm not sure if I can use index() or ~~.
TIA!