"That is perfect, ..."

Well, not quite! :-)

I saw your meditation after I read, and responded to, your OP in this thread. I now see where the Chinese characters come from; although, I still think they're superflous in the context of this specific question.

The focus of my answer was the 'r' modifier (in response to your "possible ... in one step?"). I probably should have paid more attention to your regex (/^\s+|\s+$/), rather than just copying it verbatim. With the Chinese issue out of the way, and having spent some time looking more closely at what I wrote, here's some clarifications and corrections.

The substitution example with the Chinese characters should have included a 'g' modifier. I'm now reasonably certain that wasn't what you wanted; however, it should have been written like this:

$ perl -Mutf8 -C -E 'say join " ", split //, " 北亰 " =~ s/^\s+|\s+$//gr'
北 亰

I was correct in not using the 'g' modifier in the other two substitution examples; however, I should have also removed the alternation. As the two examples splitting "1234" clearly demonstrate, there's no trailing whitespace: you only need to remove the leading whitespace. For those examples, these would have been better:

$ perl -E 'say join(" ", split /(..)/, "e58c97e4bab0") =~ s/^\s+//r' e5 8c 97 e4 ba b0 $ perl -E 'say join(" ", split /(..)/, "e58c97e4bab0") =~ s/^\s+//r =~ + y/ / /rs' e5 8c 97 e4 ba b0

Now, hopefully, it's "perfect". :-)

— Ken


In reply to Re^3: How to split, join and trim leading / leading white space by kcott
in thread How to split, join and trim leading / trailing white space by thanos1983

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.