#!/usr/bin/perl -l # use strict; use warnings; my @strings = qw{ a123ga123b a123bdfda123 a123effa123 a123 d663h }; my $len; my $rsLen = \$len; my $rxCondMatch = qr {(?x) a (\d+) (?{ print q{digits at end of string} if pos() == $$rsLen }) (??{ if ( pos() != $$rsLen ) { q{b} } }) }; foreach my $string ( @strings ) { $len = length $string; print $string; print q{Match} if $string =~ $rxCondMatch; print q{-} x 20; } #### a123ga123b Match -------------------- a123bdfda123 Match -------------------- a123effa123 digits at end of string Match -------------------- a123 digits at end of string Match -------------------- d663h --------------------