in reply to Re^2: How to split, join and trim leading / leading white space
in thread How to split, join and trim leading / trailing white space

"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

Replies are listed 'Best First'.
Re^4: How to split, join and trim leading / leading white space
by Anonymous Monk on Sep 07, 2017 at 05:48 UTC
    say join(" ", split /(..)/, "e58c97e4bab0") =~ s/^\s+//r =~ y/ / /rs;
    Augh! No! split /(..)/ makes a list with empty strings in it that you don't want. Those result in extra spaces when you join, which you then have to mop up with silly regexes. Use one of these instead:
    say join(" ", "e58c97e4bab0" =~ /(..)/g); say join(" ", unpack "(a2)*", "e58c97e4bab0");
      A reply falls below the community's threshold of quality. You may see it by logging in.