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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |