#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    378
    Rep Power
    11

    Find largest number in string


    Hey all,

    I am parsing a string that will have a series of numbers in it, e.g. 33,45,55,61,75 etc. I need to find only the largest number in that string.

    I can return the numbers no problem (and have experience with regex) but I don't want to have to write code to work out the largest value if I can use regex to do this.

    Is this possible? I haven't found anything on the web to say it's possible.

    Many thanks
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    Regexps are designed for pattern matching in text, and are not suited to numeric calculations. Even though some languages allow you to embed code inside a regexp, this wouldn't be a good idea in this case IMO as it would be ugly, confusing and overkill anyway (since the amount of pattern matching is negligible).

    I don't know what language you're programming in, but if this was me (working in Perl with no extra modules), I'd just do:
    Code:
    my $string = '33,45,55,61,75';
    my $max = 0;
    for ( split /,/, $string ) {
       $max = $_ if $_>$max;
    }
    print "Max is $max\n";
    If you have some sort of 'max' reduction built-in then that's even better (e.g. "max" function in Perl's List::Util module, "max" method in Java's Collections class).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    378
    Rep Power
    11

    Talking


    It's kinda what I thought. Thank you for the response. I'm using C# but have used perl so understand what you wrote.

    No worries, you're right, it wasn't perhaps the best method out there of determining numeric...

    Thank you for your response
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    If your string is a comma-separated list, I wouldn't use regex for this at all. Perl, python, php, and probably C# all have the "explode" function, which turns a delimited list into an array. Couple that with a built-in MAX function (which PHP has, but maybe not the others) and you get your highest number.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Brisbane, Australia
    Posts
    20
    Rep Power
    0
    No need for regex at all.

    Code:
    "33,45,55,61,75".Split(',').Max()

    Comments on this post

    • ManiacDan agrees : Right, this is the kind of thing I meant, but it's different for every language.

IMN logo majestic logo threadwatch logo seochat tools logo