好的,这就是我所拥有的:
--- 经过多次不同的尝试,我终于能够得到一些有用的东西。见下文:
BEGIN { require 5.8.0; }
use strict;
use warnings;
# string to test regular expressions
my $test_string = '<Value type="string" argument="400teets,localhost,localhost,engine1,engine2,engine50,engine100,100afdasfdas"/></Entry>';
# print out the initial string
print "The initial string is: $test_string\n\n";
# first set of arguments - all words that have a comma after them
my @first_words = ($test_string =~ /(\w+),/g);
# print first set of arguments
print "\nFirst set of arguments found\n";
foreach my $word (@first_words) {
print "$word\n";
}
# second set of arguments - all words that have a comma before them
my @last_words = ($test_string =~ /,(\w+)/g);
#print second set of arguments
print "\nSecond set of arguments found\n";
foreach my $word (@last_words) {
print "$word\n";
}
#merge the sets by popping the last element off of last_words array and pushing it into the first_words array
push(@first_words,pop(@last_words));
#print the results
print "\nMerged Sets\n";
foreach my $word (@first_words) {
print "$word\n";
}
# END OF PROGRAM
--- 真的,如果你排除所有的打印语句和 cmets,你真正需要的就是这三行:
my @first_words = ($test_string =~ /(\w+),/g);
my @last_words = ($test_string =~ /,(\w+)/g);
push(@first_words,pop(@last_words));
--- 这里是输出:
初始字符串为:
找到第一组参数
400条
本地主机
本地主机
引擎1
引擎2
引擎50
引擎100
找到第二组参数
本地主机
本地主机
引擎1
引擎2
引擎50
引擎100
100afdasfdas
合并集
400条
本地主机
本地主机
引擎1
引擎2
引擎50
引擎100
100afdasfdas