Substituting a backslashed with two backslashes 
Author Message
 Substituting a backslashed with two backslashes

Being new to Perl and not having a ton of time to mess around, what regular
expression can be used to convert the below string from source to destination?
I can easily substitute forward slashes, but backward slashes present a
problem. Thanks for any help regarding this matter.

 $source = "c:\users\working\code\bin";
 $dest = "c:\\users\\working\\code\\bin";

Also, if there is a more pertinant perl news group for this type of question,
please respond.

Mike Stopfer
Senior Software Engineer
Fisher-Rosemount Systems, Inc.



Sun, 11 Apr 1999 03:00:00 GMT  
 Substituting a backslashed with two backslashes


[lines broken to fit]

Quote:
> Being new to Perl and not having a ton of time to mess around,
> what regular
> expression can be used to convert the below string from source
> to destination?
> I can easily substitute forward slashes, but backward slashes
> present a
> problem. Thanks for any help regarding this matter.

>  $source = "c:\users\working\code\bin";
>  $dest = "c:\\users\\working\\code\\bin";

> Also, if there is a more pertinant perl news group for this type
> of question,
> please respond.

> Mike Stopfer
> Senior Software Engineer
> Fisher-Rosemount Systems, Inc.


Let me take a couple of minutes to give you a few pointers.  Please,
take these as advice to save you grief in the future and to enhance
your interactions with people on Usenet.

First of all - you did hit the right group for a Perl language
question.  However, there are a few things about your posting that are
likely to rub peoples' fur the wrong way.

Your subject uses some... creative grammar.  You could do with out the
terminal "ed" on "backslashed".  Seemingly trivial, but people are
less likely to take the time to read your posting if they either can't
understand the subject line, or think you haven't invested care in
composing it.  It's like the cover of a book.  No, we shouldn't judge
a book by its cover, but a crappy cover may prevent us from even
reading it in order to judge it.

Your first sentence is not well considered.  You don't have time to
mess around, so you expect us to take the time to mess around for you?
You've got to be kidding.  

Your question is elementary.  Generally speaking, it's good practice
to give the manual a good look-through before coming to a Usenet
newsgroup to ask for help.  It shows that you've spent some of your
time trying to solve your problem before asking someone else to do
so.  That's just common courtesy.  What's more, it's likely to get you
an answer much more quickly.  In this case, the perlre(1) manual page
treats this issue in detail.

In the future, please, be considerate of the people you're asking to
do some work for you.  Show that you've at least spent some time on
your own trying to solve your problem.  Much effort has been expended
in writing and distributing documentation with Perl.  Use it.  You're
a self-styled (as far as we know) Senior Software Engineer.  Surely
the phrase "RTFM" is familiar to you?

-Dean
--

Biological Sciences, Univ. of South Carolina, Columbia SC 29208 (803-777-3936)
PGP ID=768/22A1A015 Keyprint=2D 53 87 53 72 4A F2 83  A0 BF CB C0 D1 0E 76 C0



Sun, 11 Apr 1999 03:00:00 GMT  
 Substituting a backslashed with two backslashes

From the paths you gave, it looks like you're using DOS or some Windows
version.
If you're using hip's Win32 port of perl, you can use UNIX style
directory slashes:
eg, c:/users/working/code/bin, for in-script functions.  If you want to
escape out to the shell with the path, that won't work.

try

sub double_up {
        my $dirstring = shift;
        $dirstring =~ s/\\/\(!backslash!\)/g;
        $dirstring =~ s/\(!backslash!\)/\\\\/g;
        return $dirstring;

Quote:
}

# $new = double_up $old;

or even
sub rdouble_up {
        $_[0]  =~ s/\\/\(!backslash!\)/g;
        $_[0] =~ s/\(!backslash!\)/\\\\/g;

Quote:
}

# rdouble_up($path);



Quote:
> Being new to Perl and not having a ton of time to mess around, what
regular
> expression can be used to convert the below string from source to
destination?
> I can easily substitute forward slashes, but backward slashes present
a
> problem. Thanks for any help regarding this matter.

>  $source = "c:\users\working\code\bin";
>  $dest = "c:\\users\\working\\code\\bin";

> Also, if there is a more pertinant perl news group for this type of
question,
> please respond.

> Mike Stopfer
> Senior Software Engineer
> Fisher-Rosemount Systems, Inc.




Sun, 11 Apr 1999 03:00:00 GMT  
 Substituting a backslashed with two backslashes

                                                           ^^^^^^^^^^^^^^
Fix your newsreader; it'll make it easier for people to cc: you via e-mail.  :)

Quote:
> $source = "c:\users\working\code\bin";
> $dest = "c:\\users\\working\\code\\bin";

Check out substitution -- the s///; construct.

s/PATTERN/REPLACEMENT/egimosx
       Searches a string for a pattern, and if found, replaces that
       pattern with the replacement text and returns the number of
       substitutions made.
...etc.

I.e.,

$source = "c:\users\working\code\bin";
$dest = $source;
$dest =~ s/\\/\\\\/g;

Also, quotemeta() would do the trick as well, though it would change other
things as well.

quotemeta EXPR
       Returns the value of EXPR with with all regular expression
       metacharacters backslashed. This is the internal function
       implementing the \Q escape in double-quoted strings.

I.e.,

$dest = quotemeta ($source);

Hope this helps,
Kendall

--
Kendall P. Bullen

   Web:  http://www.his.com/~kendall/



Mon, 12 Apr 1999 03:00:00 GMT  
 Substituting a backslashed with two backslashes

Quote:

> sub double_up {
>    my $dirstring = shift;
>    $dirstring =~ s/\\/\(!backslash!\)/g;
>    $dirstring =~ s/\(!backslash!\)/\\\\/g;
>    return $dirstring;
> }

Why go to that much work?

        $dirstring =~ s/\\/\\\\/g;

--



Mon, 12 Apr 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Substituting backslash for 2 forward slashes

2. Regex problem with two backslashes

3. split and substitute, substitute, substitute

4. Entering backslashed values into variables when read in from files

5. Comparison of two fields in two text files

6. passing two arrays two a sub

7. Combining two files into a two-column file

8. s/\\/\$\\backslash\$/g : How to map \ --> $\backslash$?

9. control-backslash in a literal string

10. Why does shellwords eat backslashes?

11. Where is the document for \ (backslash operator for creating a reference)

12. How to use split function to split on a backslash

 

 
Powered by phpBB® Forum Software