PDA

View Full Version : get only numbers from a string with Perl


crmpicco
03-13-2007, 05:52 PM
Does anyone know a way i can pick out only the numbers from a string?
string examples....
TEX12900
UH8900
FloNumber899090
YB28999

I only want, say,....
12900
8900
899090
8999

Thanks,
Picco

KevinADC
03-13-2007, 08:11 PM
my $string = 'test12345';
my ($digits) = $string =~ /(\d+)/;
print $digits;

miller
03-15-2007, 03:13 AM
And to get all numbers, add the 'g' modifier to the regex:


my $string = "TEX12900\nUH8900\nFloNumber899090\nYB28999";
my @numbers = $string =~ /(\d+)/g;
print @numbers; # Equals "12900 8900 899090 28999"


- Miller