Removing spaces between single letters

后端 未结 8 2132
一向
一向 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:31

    This should do the trick:

    my $str = ...;
    
    $str =~ s/ \b(\w) \s+ (\w)\b /$1$2/gx;
    

    That removes the space between all single nonspace characters. Feel free to replace \S with a more restrictive character class if needed. There also may be some edge cases related to punctuation characters that you need to deal with, but I can't guess that from the info you have provided.

    As Ether helpfully points out, this fails on one case. Here is a version that should work (though not quite as clean as the first):

    s/ \b(\w) ( (?:\s+ \w\b)+ ) /$1 . join '', split m|\s+|, $2/gex;
    

    I liked Ether's test based approach (imitation is the sincerest form of flattery and all):

    use warnings;
    use strict;
    use Test::Magic tests => 4;
    
    sub clean {
        (my $x = shift) =~ s{\b(\w) ((?: \s+ (\w)\b)+)}
                            {$1 . join '', split m|\s+|, $2}gex;
        $x
    }
    
    test 'space removal',
      is clean('ab c d')       eq 'ab cd',
      is clean('a bcd e f gh') eq 'a bcd ef gh',
      is clean('a b c')        eq 'abc',
      is clean('abc d')        eq 'abc d';
    

    returns:

    1..4
    ok 1 - space removal 1
    ok 2 - space removal 2
    ok 3 - space removal 3
    ok 4 - space removal 4
    

提交回复
热议问题