Is there a Perl function to turn a string into a regexp to use that string as pattern?
perl, regex, string
Solution
The standard way is to use the `\Q` escape indicator before your variable, to tell Perl not to parse the contents as a regular expression:
return grep (!/\Q$filter/, @output);
Altering that line in your code yields:
1..3
ok 1 - grep, pattern not found
ok 2 - grep, pattern found
ok 3 - grep, pattern found
Problem
I have trouble using Perl grep() with a string that may contain chars that are interpreted as regular expressions quantifiers. I got the following error when the grep pattern is "g++" because the '+' symbols are interpreted as quantifiers. Here is the output of for program that follows: ``` 1..3 ok 1 - grep, pattern not found ok 2 - grep, pattern found Nested quantifiers in regex; marked by <-- HERE in m/g++ <-- HERE / at escape_regexp_quantifier.pl line 8. ``` Is there a modifier I could use to indicate to grep that the quantifiers shall be ignored, or is there a function that would escape the quantifiers ? ``` #! /usr/bin/perl sub test_grep($) { my $filter = shift; my @output = ("-r-xr-xr-x 3 root bin 122260 Jan 23 2005 gcc", "-r-xr-xr-x 4 root bin 124844 Jan 23 2005 g++"); return grep (!/$filter/, @output); } use Test::Simple tests => 2; ok(test_grep("foo"), "grep, pattern not found"); ok(test_grep("gcc"), "grep, pattern found"); ok(test_grep("g++"), "grep, pattern found"); ``` PS: in addition to the answer question above, I welcome any feedback on Perl usage in the above as I'm still learning. Thanks