in reply to Help with regex, how to get the largest integer in a string?

Since There's always MTOWTDI (and I'm experimenting with look-ahead assertions):

#!/perl/bin/perl -w use strict; use Data::Dumper; use List::Util qw/ max /; my $s = "ASBSDEC 34 GADVVEEVEETTE 56 IOEOREAK GKJEOG EFEAF 1090 DAFFEE + 376"; # use a look-ahead assertion to split string into array of strings and + numbers # the corresponding string occupies the index immediately preceding th +e number my @arr = split /(\D+)(?=\d+)/, $s; # grep only for the numbers in the list, selecting the largest one print max(grep { /^\d+$/ } @arr), "\n"; __OUTPUT__ 1090

Update: Corrected typo diligently found by liverpole++


Where do you want *them* to go today?

Replies are listed 'Best First'.
Re^2: Help with regex, how to get the largest integer in a string?
by MaxKlokan (Monk) on Apr 19, 2007 at 08:45 UTC
    Just a question: What is the lookahead needed for?
    It seems to mee that
    my @arr = split /(\D+)/, $s;
    works as well.
    Max
      Whenever split() will do, I avoid regex. ++

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!