Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by thezip (Vicar)
on Apr 19, 2007 at 00:44 UTC ( [id://610877]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://610877]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 19:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found