in reply to arithmetic in a regex ?

It is possible using the postponed regular subexpression.
/([0-8])[a-z](??{ $1 + 1 })/

See perlre for details.

Updated: Simplified, don't use named capture groups.

#!/usr/bin/perl use warnings; use strict; use Test2::V0 '-no_srand' => 1; my @cases = ( ['13s4', '3s4'], ['3s44', '3s4'], ['0a1', '0a1'], ['0a2', undef], ['9y10', undef] ); for my $case (@cases) { my ($line, $expected) = @$case; my $result; $result = $& if $line =~ /([0-8])[a-z](??{ $1 + 1 })/; is $result, $expected, $line; } done_testing;

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: arithmetic in a regex ?
by Anonymous Monk on Nov 13, 2023 at 11:00 UTC

    Thats brilliant.

    Now I have something to work on.

    MANY THANKS