mis has asked for the wisdom of the Perl Monks concerning the following question:
Hi Perlmonks,
Is if possible to have a string interpreted as a literal without pre-escapting in a regex. Consider:
This will return:#!/opt/local/bin/perl my $a = "Tick F***ing Tock"; my $b = "Friday night at 11:30pm the start of a new series, 'Tick F*** +ing Tock' explores..."; if ($b =~ /$a/) { print "..do something..\n"; }
Of course this will work:$ ./t.pl Nested quantifiers in regex; marked by <-- HERE in m/Tick F** <-- HERE + *ing Tock/ at ./t.pl line 5.
Result:#!/opt/local/bin/perl my $a = "Tick F***ing Tock"; my $b = "Friday night at 11:30pm the start of a new series, 'Tick F*** +ing Tock' explores..."; $a =~ s/([\(\)\[\]\{\}\\\*\?\.\$\^\@\!\&])/\\$1/g; if ($b =~ /$a/) { print "..do something..\n"; }
However there will result in a long list of escapes to make parsing safe as '$a' in my script is dynamic and not fixed like in this example... Also there will no doubt be a significant performance hit considering the number of times I'd be looping over the regex...$ ./t.pl ..do something..
Thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Switch/Format to not interpret metacharacters in regex?
by tybalt89 (Monsignor) on Dec 04, 2019 at 01:36 UTC | |
by mis (Initiate) on Dec 04, 2019 at 01:44 UTC | |
|
Re: Switch/Format to not interpret metacharacters in regex?
by GrandFather (Saint) on Dec 04, 2019 at 01:47 UTC | |
by mis (Initiate) on Dec 04, 2019 at 02:09 UTC | |
by kcott (Archbishop) on Dec 04, 2019 at 06:30 UTC | |
|
Re: Switch/Format to not interpret metacharacters in regex?
by Laurent_R (Canon) on Dec 05, 2019 at 17:27 UTC | |
by afoken (Chancellor) on Dec 05, 2019 at 20:44 UTC | |
by Laurent_R (Canon) on Dec 06, 2019 at 09:10 UTC |