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.
In reply to Switch/Format to not interpret metacharacters in regex? by mis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |