in reply to Re^2: Disable Regex
in thread Disable Regex

If your string is coming from an external source (read from a file, read from the command line, STDIN, a database field) you shouldn't need to worry about escaping \ * ?, f.e.:


qt.pl:
#!/usr/bin/perl use strict;use warnings; my $string = $ARGV[0]; my $test = $string; my @illegal = qw(\ * ?); my @legal = qw(bs a q); my $c = 0; foreach my $val (@illegal) { $test =~ s/\Q$val\E/[$legal[$c]]/g; $c++; } print $test."\n";

$ perl ./qt.pl 'a\\b\\c\d\\\\e' a[bs][bs]b[bs][bs]c[bs]d[bs][bs][bs][bs]e $ perl ./qt.pl 'foo*bar\eleven?three' foo[a]bar[bs]eleven[q]three $

the reason you needed to escape \ * ? is that you were entering the assignment in perl, and perl was doing the interpolation. This won't happen in a already assigned string.

Replies are listed 'Best First'.
Re^4: Disable Regex
by SavannahLion (Pilgrim) on Aug 26, 2009 at 07:26 UTC

    Really?! Buggers... so it will work that way. To find time to work on the problem, I simply scanned through the file until I found a section causing the headache and copied it into a perl file and assigned it to a variable (too lazy to create a file, open then read it I guess). Then I moved the sample+Perl file to a different computer (my laptop) so I can work away from the main computer.

    I'll read it directly from a file and see how it goes.

      You might like the __DATA__ section:

      use strict; use warnings; foreach (<DATA>) { chomp; print "$_\n";; } __DATA__ illegal\characters*example? ab\delta.txt ab*delta.txt

      Which produces

      illegal\characters*example? ab\delta.txt ab*delta.txt