Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I keep only the numbers in a variable in Perl?

Tags:

regex

perl

I have a variable like this below:

G12345(@@)

How can I keep in the variable only the numbers 12345. I have done it before in PHP but I cannot find a way in Perl.

like image 943
Nidis Avatar asked Oct 21 '25 14:10

Nidis


2 Answers

$v =~ s/\D//g; should do the trick.

(Regular expression substitute "Not a number" with "nothing", globally)

like image 91
Quentin Avatar answered Oct 23 '25 07:10

Quentin


This can also be done without regular expressions: Transliterate: tr///

use warnings;
use strict;

my $s = 'G12345(@@)';
$s =~ tr/0-9//cd;
print "$s\n";

__END__

12345
like image 41
toolic Avatar answered Oct 23 '25 05:10

toolic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!