舉例說明:
1. 標量:
匹配成功返回1,匹配失敗返回0;
my $res = ($string=~/value='(\w+)'/);
print "$res\n";
output:
1
2. 列表:
use Data::Dumper;
my $string = "hello perl";
my @perls = $string =~ m/hello/ig ;
print Dumper @perls;
output:
$VAR1 = 'hello';
$string = "name='ns' value='CSDN'";
my ($res) = ($string=~/value='(\w+)'/);
print "$res\n";
output:
CSDN
3. 哈希:
$string = "name=xxyy leval=9 score=0";
%hash = {leval=>"12"};
%hash = $string =~ /(\w+)=(\w+)/g;
print Dumper(\%hash); #先按照列表匹配獲得值,然後再通過列表對hash賦值
output:
$VAR1 = {
'name' => 'xxyy',
'level' => '9', (修改了hash裏面原來的password的值)
'score' => '0'
};