Removing spaces between single letters

后端 未结 8 2131
一向
一向 2021-01-07 06:01

I have a string that may contain an arbitrary number of single-letters separated by spaces. I am looking for a regex (in Perl) that will remove spaces between all (unknown n

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-07 06:21

    Your description doesn't really match your examples. It looks to me like you want to remove any space that is (1) preceded by a letter which is not itself preceded by a letter, and (2) followed by a letter which is not itself followed by a letter. Those conditions can be expressed precisely as nested lookarounds:

    /(?<=(?

    tested:

    use strict;
    use warnings;
    
    use Test::Simple tests => 4;
    
    sub clean {
      (my $x = shift) =~ s/(?<=(?

    output:

    1..4
    ok 1
    ok 2
    ok 3
    ok 4
    

    I'm assuming you really meant one space character (U+0020); if you want to match any whitespace, you might want to replace the space with \s+.

提交回复
热议问题