in reply to Re^2: Regexp: How to match in middle but not the ends?
in thread Regexp: How to match in middle but not the ends?

No, he shouldn't use "+", since he doesn't want to strip any sequence of "L". For example, your solution fails to match "LL" in "---LLLL---".

My solution was lacking since I wasn't checking for an L at the start or end of the string. Fixes:

$string =~ s/-L|L-/-/g; $string =~ s/^L//; $string =~ s/L$//;
or
$string =~ s/^L|(?<-)L|L(?=-)|L$//g;
or
# Does a bit more than stripping, but in an inconsequential fashion. $string =~ s/^L|-L|L-|L$/-/g;

Replies are listed 'Best First'.
Re^4: Regexp: How to match in middle but not the ends?
by Skeeve (Parson) on Jul 30, 2006 at 20:10 UTC

    It's debatable: The OP said, the strings may not start or end with L. He didn't say which strings: Those, that are returned (use +) or those that are found (don't use +).

    I vote for the strings returned ;-) But only the OP can tell.

    P.S. See his (partial) Solution, He's cutting of all leading L's, so I have a good argument for my view, don't you agree?


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e