1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#!perl
use Test::More tests => 19;
my $str = "foo bar baz";
my @expected = split / /, $str;
{
use re::engine::RE2 -strict => 1;
my @matches = $str =~ /(\w+)/g;
is_deeply \@matches, \@expected;
is pos $str, undef;
my $i = 0;
while($str =~ /(\w+)/g) {
is $1, $expected[$i++];
is pos $str, $i-1+3*$i;
}
pos $str = 2;
ok ~~($str =~ /\b(\w+)/g);
is $1, $expected[1];
ok ~~($str =~ /\b(\w+)/g);
is $1, $expected[2];
$str =~ /(?P<a>\w+) (?P<b>\w+) (?P<c>\w+)/ && pass "Matched";
is_deeply \%+, { a => "foo", b => "bar", c => "baz" };
# Captures retained?
ok !($str =~ /(?P<a>\w+) meh (?P<c>\w+)/);
is_deeply \%+, { a => "foo", b => "bar", c => "baz" };
ok $str =~ /(?P<a>\w+) (?P<d>\w+)/;
is_deeply \%+, { a => "foo", d => "bar" };
is_deeply qr/(?P<a>\w+) (?P<d>\w+)/->named_captures, { a => 1, d => 2 };
}
|