s/// stands for substitution operation, not sed. sed(1) is not synonymous to substitution, even though that may be its most used feature. Could you please update the title of your post to "2 substitution problems"?

/vobs/dirname1////dirname2//dirname3////filename becomes /vobs/dirname1/dirname2/dirname3/filename

I have tried to replace 2 or more "/"'s with a single "/" using

$filename =~ s/\/[\/]+/\//;

but the above statement does nothing

That last statement is false, which you would have noticed if you had printed the file name before & after s/// operation. You would have seen that at least first set of consecutive '/' is squashed to single one. To flatten all the rest, all you need to do is globally replace by using /g flag ...

# Used a delimiter other than '/' to avoid escaping '/'. $p =~ s!//+!/!g;

As for your second problem, the subdirectory needs to be captured to reference in the pattern (and later in substitution) ...

$p = '/vobs/synergy_core_apps/code/../code/personalize/src'; print 'before: ' , $p , "\n"; # Besides the /g flag, used /x in order to make the pattern stand out +. $p =~ s{ ([^/]+) /[.]{2}/ \1 } /$1/gx; print 'after: ' , $p , "\n";

If the paths being cleansed actually exist on the file system, I would rather use &Cwd::realpath.

See also: perlretut, perlre, YAPE::Regex::Explain, perlfaq6, Mastering Regular Expressions, etc.


In reply to Re: 2 substitution problems by parv
in thread Yet another substitution problem by Amphiaraus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.