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

Hello Forks, Please i want to remove one if there is // in file path prefix.

$test = "//user/"; $test =~ s/^\///; print $test."\n";
the problem is the $test could be /user/ then i will have user/ which is not what i want to see happen.
thank you for your help.
The beginning of knowledge is the discovery of something we do not understand.
    Frank Herbert (1920 - 1986)

Replies are listed 'Best First'.
Re: help with regex
by choroba (Cardinal) on Mar 08, 2016 at 23:04 UTC
    You don't want to remove a slash, you want to replace two slashes by a single one. Use a different delimiter to avoid the need of backslashing:
    $test =~ s{^//}{/};

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: help with regex
by ExReg (Priest) on Mar 08, 2016 at 23:35 UTC

    Or, expanding on what choroba said,

    $test =~ s{^/+}{/};

    if, for some perverse reason, you have more than two //, like $test = "////user/";

      It's probably a nice addition but I find regexes that can work while leaving the original unmodified irksome. $test =~ s{^/+}{/} will work on /something and leave it identical. I would rather see $test =~ s{^//+}{/}; so the intent is clear and action is only taken when necessary.