我需要找到刺中有多少10和01秒。对于ex:10101在这里,两个10在那里,两个01就像使用reg ex并找到它?打印10匹配2次,01匹配2次
答案 0 :(得分:2)
使用goatse operator =()=
:
$string = '10101';
$a =()= $string =~ m/10/g;
$b =()= $string =~ m/01/g;
print "a: $a\nb: $b\n";
输出结果为:
a: 2
b: 2
答案 1 :(得分:1)
这是我的解决方案:
use strict;
use warnings;
my $test = "101010";
my @matches_10 = $test =~ m!10!g;
my @matches_01 = $test =~ m!01!g;
print "matches 10: ", scalar(@matches_10), "\n"; #<-- prints: 3
print "matches 01: ", scalar(@matches_01), "\n"; #<-- prints: 2