How do I replace “\” with “//” in Perl?
-
18-09-2019 - |
Question
I want to substitute, the "\" that appears in the Windows directory link to a "/".
I tried using s//\////g
, but it doesn't seem to do the trick.
Solution 3
I think this should do it:`
$str =~ s{\\}{//}g;
OTHER TIPS
s[\\][//]g
- \ needs to be escaped in a regex
- / does not
- Avoid using / to delimit regex sections when using / in the expression itself (it makes things much more readable!)
... but you should probably use something like Path::Class.
First of all, using a different separator than \
will make your regex more readable.
Then you have to replace the \
with \\
, or it will be used to escape the following character (a /
in the regex you are using).
$link =~ s|\\|//|g;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow