The part that I can't quite get to work is the conditional as it's always failing:
use Test::More tests => 2;
my $regex = qr/(\d+),(\d+)
(?(?{\g1<\g2})(*FAIL))
/x ;
like( "(23,36)", $regex, 'should match' );
unlike( "(36,23)", $regex, 'should not match' );
not ok 1 - should match
# Failed test 'should match'
# at - line 7.
# '(23,36)'
# doesn't match '(?^x:(\d+),(\d+)
# (?(?{\g1<\g2})(*FAIL))
# )'
ok 2 - should not match
# Looks like you failed 1 test of 2.
$1 equals the text " brown ".
The $ number language element includes the last substring matched by the number capturing group in the replacement string, where number is the index of the capturing group. For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.
Digit \d[0-9]: The \d is used to match any digit character and its equivalent to [0-9]. In the regex /\d/ will match a single digit.
Your code needs the following fixes:
$1
and $2
variables in the experimental (?{ })
code block.(*SKIP)
control verb to prevent backtracking explicitly.The code:
use strict;
use warnings;
use Test::More tests => 2;
my $regex = qr/(\d+),(\d+)
(?(?{$1 > $2})(*SKIP)(*FAIL))
/x ;
like( "(23,36)", $regex, 'should match' );
unlike( "(36,23)", $regex, 'should not match' );
Outputs:
1..2
ok 1 - should match
ok 2 - should not match
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With