G'day thanos1983,

"Is it possible to be done in one step?"

If you're using Perl 5.14, or later, you can chain those operations using the 'r' modifier. See "perl5140delta: Non-destructive substitution".

It's somewhat unclear what you're actually trying to achieve here. The use of Chinese characters seems superfluous to the actual question asked. The use of the 'g' modifier on the substitution, together with the '^' and '$' assertions, makes me wonder if you're perhaps dealing with multiline strings; however, the absence of the 'm' modifier suggests otherwise.

Here's some guesses as to the type of thing you might want:

$ perl -Mutf8 -C -E 'say join " ", split //, "北亰"'
北 亰

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

If you're simply unfamiliar with what's going on with split, that's explained at the end of that documentation: "If the PATTERN contains capturing groups, ...".

$ perl -E 'my @x = split /(..)/, "1234"; say "|$_|" for @x' || |12| || |34| $ perl -E 'my $x = join "_", split /(..)/, "1234"; say $x' _12__34

Update (additional information): As an additional example, to extend that last chaining example, you could do this to reduce multiple embedded spaces to a single space:

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

See "perlop: y/SEARCHLIST/REPLACEMENTLIST/cdsr" for more about that.

Update (further discussion): See my subsequent response (below) for further discussion and "some clarifications and corrections".

— Ken


In reply to Re: 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.