Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: how to overcome Win32 path delimiter in s///?

by The Mad Hatter (Priest)
on Oct 14, 2004 at 23:32 UTC ( #399358=note: print w/ replies, xml ) Need Help??


in reply to how to overcome Win32 path delimiter in s///?

Wrap $path in \Q and \E (which escapes metachars) like so...

my $file = 'c:\some\win32\path\mydir\file.txt'; my $path = 'c:\some\win32\path'; (my $file_nopath_noext = $file) =~ s/\Q$path\E\\(.*)\.txt/$1/; print $file_nopath_noext."\n";

See perldoc perlre.


Comment on Re: how to overcome Win32 path delimiter in s///?
Download Code
Re^2: how to overcome Win32 path delimiter in s///?
by bsdz (Friar) on Oct 14, 2004 at 23:41 UTC
    that's precisely what I was hoping for. I failed to consider \Q..\E metacharacter manipulation. Thank you
      The function quotemeta will acomplish the same thing if you use it on the value before it is interpolated by the regex.

      If you are particularly worried about forward slashes then you can just use an alternate character to delimit the parts of the substitution.
      e.g. single char s!!! or paired s()()

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://399358]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2014-09-20 10:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls