我有一个字符串,如下所示。
我希望将此输出为:
field1=1 field2=2 field3=abc
我希望将此输出为:
2,1,abc
有什么想法可以帮我完成这个任务吗?我可以编写一个小的C或Java程序来实现,但我正在尝试寻找一种在Perl中简单实现它的方法。
use strict;
use warnings;
my $string = 'field1=1 field2=2 field3=abc';
my @values = ($string =~ m/=(\S+)/g);
print join(',', @values), "\n";
#!/usr/bin/perl
use strict;
use warnings;
# Input string
my $string = "field1=1 field2=2 field3=abc";
# Split string into a list of "key=value" strings
my @pairs = split(/\s+/,$string);
# Convert pair strings into hash
my %hash = map { split(/=/, $_, 2) } @pairs;
# Output hash
printf "%s,%s,%s\n", $hash{field2}, $hash{field1}, $hash{field3}; # => 2,1,abc
# Output hash, alternate method
print join(",", @hash{qw(field2 field1 field3)}), "\n";
在列表上下文中使用m//g
:
#!/usr/bin/perl
use strict;
use warnings;
my $x = "field1=1 field2=2 field3=abc";
if ( my @matches = $x =~ /(?:field[1-3]=(\S+))/g ) {
print join(',', @matches), "\n";
}
__END__
输出:
C:\Temp> klm
1,2,abc
$_='field1=1 field2=2 field3=abc';
$,=',';
say /=(\S+)/g
perl -E "$_='field1=1 field2=2 field3=abc';$,=',';say /=(\S+)/g"
- Michael Carman有几种方法可以做到这一点:
my $s = "field1=1 field2=2 field3=abc"; $s =~ /field1=(\w*) field2=(\w*) field3=(\w*)$/; //pick out each field print $1,$2,$3;'
12abc
my $s = "field1=1 field2=2 field3=abc"; my @arr = split / /, $s; print @arr,"\n"; //make an array of name=value pairs my @vals = map { @pairs = split /=/, $_; $pairs[1] } @arr; //get the values only from each pair print @vals'
field1=1field2=2field3=abc
12abc
my $s = "field1=1 field2=2 field3=abc"; my @arr = split / /, $s; my %pairs = map { split=/, $_; } @arr; print $pairs{field1}, $pairs{field2}, $pairs{field3}
12abc
my $str = 'field1=1 field2=2 field3=abc';
print(join(',', map { (split('=', $_))[1] } split(' ', $str)));
假设您的排序是一个打字错误:
#!/usr/bin/perl
use strict; use warnings;
my $str='a=1 b=2 c=abc';
my @v;
while ($str =~ /=(\S+)/g) {
push @v, $1;
}
print join (',', @v);
m//g
一次捕获所有匹配项。 - Michael Carmanmy $a = "field1=1 field2=2 field3=abc";
my @f = split /\s*\w+=/, $a;
shift(@f);
print join(",", @f), "\n";
$string="field1=1 field2=2 field3=abc";
@s=split /\s+/,$string;
$temp=$s[1];$s[1]=$s[0];$s[0]=$temp;
foreach (@s){s/.*=//; push(@a,$_ );}
print join(",",@a);
=
"两边的内容,并将其放入哈希表中。use strict;
use warnings;
my $str = 'field1=1 field2=2 field3=abc';
my %fields = $str =~ / (\S+) \s* = \s* (\S+) /xg;
use YAML;
print Dump \%fields
--- field1: 1 field2: 2 field3: abc
如需更多信息,请阅读{{link1:perldoc perlre
}}。
如果您是初学者,您可能想阅读{{link2:perldoc perlretut
}}。
1
和 **2
**? - Sinan Ünür