ioiioi has asked for the wisdom of the Perl Monks concerning the following question:

$strInputFileName = D:\direcorty\file.ext #I would like to use regular expression to change it to $strOutputFileName = \'D:\\\\direcorty\\\\file.ext\'
I am a newbie, can anyone help me? thanks in advance.

Replies are listed 'Best First'.
Re: how to write this Regular Expression?
by diotalevi (Canon) on Sep 19, 2005 at 14:35 UTC

    This is slightly more direct code won't produce exactly what you asked for but I think it gets to the meaning you're actually looking for.

    $strOutputFilename = quotemeta qq['$strInputFilename']; # \'D\:\\direcorty\\file\.ext\'
Re: how to write this Regular Expression?
by polypompholyx (Chaplain) on Sep 19, 2005 at 15:39 UTC

    Best shot, given the poorly-posed question...

    use strict; # Single quote string, and back-whack the backslashes # to avoid misinterpretation (by readers, if not by perl). my $strInputFileName = 'D:\\direcorty\\file.ext'; # Globally replace backslashes with four backslashes. # Note that these all need backwhacking too, since # regex acts somewhat like a quoted string. $strInputFileName =~ s{\\}{\\\\\\\\}g;

    I don't know what you're doing with the escaped quotes though. Also, it's directory, not 'direcorty'.

Re: how to write this Regular Expression?
by blazar (Canon) on Sep 19, 2005 at 14:32 UTC
    The code as you posted it doesn't make much sense even if interpreted sloppily. Do you want to replace single backslashes with a sequence of four backslashes? Even when posting pseudocode like this it would be nice to adhere to Perl's syntax if it is not an overkill. Here quoting the strigs would have been nice.

    Wild guess: you may be interested to know that slashes work just fine even under Windows in Perl, and that there are various reasons why should do so anyway...

Re: how to write this Regular Expression?
by Anonymous Monk on Sep 19, 2005 at 15:00 UTC