在Perl中,如何删除前导数字之间的空格,但保留每行其余字符串的空格?我在Perl中使用正则表达式(regex),但是正则表达式的字母/符号组合给出了错误的输出。
这是两行苹果的输入:
Apples 2 Green 3 Red
Apples 5 Yellow 4 Rotten
我想删除2和Green&5和Yellow之间的空格,但保留其他空格。
所以输出应如下所示:
Apples 2Green 3 Red
Apples 5Yellow 4 Rotten
这是我的代码:
#! /usr/bin/perl
use 5.10.0;
use warnings;
my $Combine_Leading_Numbers1 = "Apples 2 Green 3 Red";
my $Combine_Leading_Numbers2 = "Apples 5 Yellow 4 Rotten";
$Combine_Leading_Numbers1 =~ s/^(\s\d);
$Combine_Leading_Numbers2 =~ s/^(\s\d);
say $Combine_Leading_Numbers1;
say $Combine_Leading_Numbers2;
答案 0 :(得分:1)
类似这样的东西:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw/say/;
my $s = "Apples 2 Green 3 Red";
$s =~ s/^(\S+\s+\d+)\s+/\1/;
say $s;
答案 1 :(得分:0)
单线:
perl -lpe 's/([0-9])\s/\1/' <(echo 'Apples 2 Green 3 Red \nApples 5 Yellow 4 Rotten')