in reply to Regex match at the beginning or end of string

Use qr to cache your independent regex's.
my $pattern1 = qr{abc:\(?.*?\)?\s}; my $pattern2 = qr{${variable}.{0,5}\s}; if ($str =~ /$pattern1(.*?)$pattern2/) { print "matched $1"; } if ($str =~ /$pattern2(.*?)$pattern1/) { print "matched $1"; }
Then the length of the regexs won't be an issue.