slugger415 has asked for the wisdom of the Perl Monks concerning the following question:
Hi all, sorry if this is a naive question but I've searched around the web and haven't found a good answer, including https://perldoc.perl.org/perlre#Metacharacters.
How do I match a regex that contains metacharacters that are used in matching, such as asterisks, parens, etc.? Without escaping each?
I have two variables set, both containing asterisks, and a regex comparison:
#! /usr/bin/perl my $v1 = "*hello"; my $v2 = "*hello"; if ($v1 =~ /$v2/){ print "match\n"; }
This obviously results in a Quantifier follows nothing in regex error. I can escape out the asterisk and it works fine:
$v2 =~ s/\*/\\\*/g;
But is there a more elegant way to do that, one that might also pick up some of the others?
thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex matching on metacharacters
by LanX (Saint) on Jul 21, 2022 at 00:06 UTC | |
by slugger415 (Monk) on Jul 21, 2022 at 03:32 UTC | |
|
Re: regex matching on metacharacters
by hippo (Archbishop) on Jul 21, 2022 at 09:15 UTC |