-1

我需要找出刺痛中有多少个 10 和 01 。例如: 10101 在这个,有两个 10 和两个 01 像那样使用 reg ex 并找到它?并打印 10 匹配 2 次, 01 匹配 2 次

4

2 回答 2

2

使用goatse 运算符 =()=

$string = '10101';

$a =()= $string =~ m/10/g;
$b =()= $string =~ m/01/g;

print "a: $a\nb: $b\n";

输出是:

a: 2
b: 2
于 2013-04-24T08:06:32.190 回答
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
于 2013-04-24T08:09:07.637 回答