Perl regex with pipes
I'm not really a pearl monk, so if you could help me digest, what does this regex do (if it's one)?
my $pathHere = "/some/path/to/file";
my $pathThere = "/some/path/";
$pathHere =~ s|$pathThere||;
Perl is not really my daily tool, so I'm pretty shy about knowing - I guess it adjusts to match the var value, but guessing is not the way - pipes are cutting me off ...
thank
+3
source to share
1 answer
In Perl, you usually use / as the delimiter in your regular expression.
$pathHere =~ s/abc/def/; # replace 'abc' with 'def'
However, you can see that this is problematic for paths as you will need to escape from everything.
$pathHere =~ s/my\/path\/here/my\/newpath\/there/;
Hence Perl allows you to specify a different delimiter as the character after the 's', hence:
$pathHere =~ s|my/path/here|my/newpath/there|;
See the documentation for details .
+5
source to share