First, your single line doesn't work because you can't string two separate s/// together like that.   What you wrote was
$directory =~ s<^/*></> . </*$></>;
Turning on warnings would've helped here.

cchampion fixed his RE so I can't explain why it doesn't work, cause now it does.

What I've been able to make work is something that recognizes the _middle_ of your string, not either end.   This is a translation out of Perl Cookbook, 2ndEd.   (cargo-cult of a higher order?)

$directory =~ s< ^ /? # match leading '/' if present ( # capture what is between ends [^/]* # match anything not a '/' (?: # might be a '/', (?! /$ ) # but don't allow a '/' at EOL . # okay, eat the '/' ) * ) /? $ # match trailing '/' > </$1/>x; # now surround the middle with '/'
Update:   I take too long typing!   And my solution only takes one '/' off each end.   I guess I took so long typing that "I forgot the question"   ;-)

In reply to Re: Regex - one and only one / at beginning and end of file path by shenme
in thread Regex - one and only one / at beginning and end of file path by mikedshelton

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.