Perl - returns the last alpha character after the last digit in the string and returns the modified string too

I have a line like this:

$var = '123456d';

or maybe

$var = '123456'; # (no alpha char)

If it matters, there will be at most 6 digits of at least 5. The numbers do not have alpha, spaces or special characters.

In my Perl script, I need to delete and return the last alpha character and a string without an alpha character.

I suppose you can use \ D and crop? or chop? but, there should be an easy way to quickly get both vars.

my $numbers =~   s/\D+?$var//;  #???  

my $alpha = substr($var, 0, -1); ## but no alpha check.

or

my $alpha = chop($var); ## but no alpha check.

then if aZ and so on to check if it is an alpha symbol.

Or the limit of my decision:

$alpha = $var;
$alpha =~ s/[0-9]//ig;
$var =~ s/[a-Z]//ig;
$numbers = $var;

therefore the result:

($numbers == '123456')
($alpha eq 'd') (or '' if nothing)

It seems to me that this is too trivial to ask here, but I just cannot find or write an applicable solution.

= ~ s - , .

...

, ! 2 -! var ($ alpha eq 'dd')

to0:

$var =~ s{^([0-9]+).*}{$1}i;
+3
2

\pL , .

my $var = '123456d';
my ($num, $letter) = $var =~ /^(\d+)(\pL)?$/;

, \w , . tchrist, \p{alpha}.

+6

, - :

my $numbers =~ s/(\d+)(\p{alpha}+)?/$1/;

my $alpha = $2;

$1 , $2 .

: \d , \p{alpha} .

0

All Articles