in reply to Disable Regex

Does this happen with real data? The reason
my $test = 'illegal\\\\characters*example?'; 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";
returns illegal[bs]characters[a]example[q] is because the two \s are actually \ as an escape and then as the escaped character. From Quote and Quote like Operators:
A single-quoted, literal string. A backslash represents a backslash unless followed by the delimiter or another backslash, in which case the delimiter or backslash is interpolated.


To have two \s in your '-delimited string use '\\\\' (two escaped backslaches)
my $test = 'illegal\\\\characters*example?'; 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";
If you are building up your string from component parts, it shouldn't be a concern.

Replies are listed 'Best First'.
Re^2: Disable Regex
by AnomalousMonk (Archbishop) on Aug 26, 2009 at 06:20 UTC
    To have two \s in your '-delimited string use '\\\\' (two escaped backslaches)
    Or else a single escaped backslash and then a single un-escaped backslash:
    >perl -wMstrict -le "my $s1 = 'a\b'; my $s2 = 'c\\d'; my $s3 = 'e\\\f'; my $s4 = 'g\\\\h'; print $s1, $s2, $s3, $s4; " a\bc\de\\fg\\h
Re^2: Disable Regex
by SavannahLion (Pilgrim) on Aug 26, 2009 at 07:04 UTC

    Sadly, no. :( I'm not building up the string in question. That was just a sample of a typical (atypical?) string I'm grabbing. Well... I lied. I am building up the path (in the above example, that would be C:\random\location), it's the names I'm not building up. Really, they literally come to me as ab\delta (I add the .txt) and ab\delta is literally going to be the name of the file or directory in the string I'm constructing. While scanning over the logs, I did find several botched names that come up as something like ab\\delta

    So if I have two strings, one as ab\delta and the other as ab\\delta. I must know how many real \s (or * or ? or whatever) there are so I can add in the appropriate number of replacements.

      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.

        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.