Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Disable Regex

by SavannahLion (Pilgrim)
on Aug 26, 2009 at 07:04 UTC ( [id://791268] : note . print w/replies, xml ) Need Help??

in reply to Re: Disable Regex
in thread Disable Regex

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.

Replies are listed 'Best First'.
Re^3: Disable Regex
by james2vegas (Chaplain) on Aug 26, 2009 at 07:11 UTC
    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.:
    #!/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 ./ 'a\\b\\c\d\\\\e' a[bs][bs]b[bs][bs]c[bs]d[bs][bs][bs][bs]e $ perl ./ '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.

        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