Is there a Perl-compatible regular expression to trim whitespace from both sides of a string?

前端 未结 13 940
醉话见心
醉话见心 2021-02-08 07:45

Is there a way to do this in one line?

$x =~ s/^\\s+//;
$x =~ s/\\s+$//;

In other words, remove all leading and trailing whitespace from a stri

13条回答
  •  盖世英雄少女心
    2021-02-08 08:22

    Funny you should bring this up!

    I recently read an article analyzing the performance of twelve (!) different trim implementations.

    Although the article specifically uses the JavaScript regex implementation, it uses Perl syntax, so I think it's apropos to this discussion.

提交回复
热议问题