You can iterate over a given string using the /g (global) quantifier, as well as regex captures:
use strict; use warnings; my $string = "ASBSDEC 34 GADVVEEVEETTE 56 IOEOREAK GKJEOG EFEAF 1090 D +AFFEE 376"; my $max; # This is undefined until set while ($string =~ /(\d+)/g) { if (!defined($max) or $max < $1) { $max = $1; } } print "Max value is $max\n";
Or you could make it into a subroutine (eg. max_val) like so:
use strict; use warnings; my $string = "ASBSDEC 34 GADVVEEVEETTE 56 IOEOREAK GKJEOG EFEAF 1090 D +AFFEE 376"; my $max = max_val($string); print "Max value is $max\n"; sub max_val { my $str = shift; my $max; while ($string =~ /(\d+)/g) { if (!defined($max) or $max < $1) { $max = $1; } } return $max; # This will be undefined if no integer found }
And, of course, you can read more about regular expressions with perlretut and perlre.
Update: Oh, and perlrequick. I always forget that one.In reply to Re: Help with regex, how to get the largest integer in a string?
by liverpole
in thread Help with regex, how to get the largest integer in a string?
by coltman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |