Share via


Regex 101 Exercise S4 - Extract load average from a string

Exercise S4 - Extract load average from a string

The shop that you work with has a server that writes a log entry every hour in the following format:

8:00 am up 23 day(s), 21:34, 7 users, load average: 0.13

You need to write a utility that lets you track the load average on an hourly basis. Write a regex that extracts the time and the load average from the string.

[Update: The time you need to extract is the "8:00 am" part...]

Comments

  • Anonymous
    November 14, 2005
    Eric,

    Which one is the time? The first one, or the second one?

    21:34 can be interpreted as military time, or is that something else, as in, the up time is 23 days, 21 hours, and 34 minutes?

  • Anonymous
    November 14, 2005
    The first bit is the time: 8:00 am
    The second bit is the "uptime" (time since last reboot)
    In this case the server has been up for 23 days, 21 hours, and 34 minutes
    In other words, it was last rebooted 10/21/2005 at 10:26 AM

  • Anonymous
    November 14, 2005
    string x = s.Substring(0, s.IndexOf(" ")) + " / " + s.Substring(s.LastIndexOf(" "));

  • Anonymous
    November 14, 2005
    Looks like a nice opportunity for named groups.

    Regex myRegex = new Regex(@"(?<time>d{1,2}:d{2}s[ap]m).average:s(?<loadaverage>d.d*)", RegexOptions.None);
    Match myMatch = myRegex.Match("8:00 am up 23 day(s), 21:34, 7 users, load average: 0.13");
    Console.WriteLine("Time: " + myMatch.Groups["time"].Value);
    Console.WriteLine("Load Average: " + myMatch.Groups["loadaverage"].Value);
    Console.ReadLine();

  • Anonymous
    November 14, 2005
    if the format is fixed,

    ^(?<time>S+sS+).:s+(?<avg>S+)s*$

    ?

  • Anonymous
    November 14, 2005
    private static Regex logParser =
    new Regex(@"^(?<time>d*:ds(?:am|pm)).loadsaverage:s(?<load>d.d)$");

    public LogEntry Match(string input)
    {
    LogEntry entry;
    if (logParser.IsMatch(input))
    {
    Match match = logParser.Match(input);
    entry = new LogEntry(match.Groups["time"].Value, match.Groups["load"].Value);
    }
    return entry;
    }

  • Anonymous
    November 14, 2005
    In Javascript syntax:

    /^(d{1,2}:d{1,2}s+(?:am|pm)).*(?:load average:s+(d+.d+))$/i

  • Anonymous
    November 16, 2005
    I would parse each separately, myself...

    Assumptions:
    Either AM/PM or 24-hour time
    Single-digit hours may or may not have a leading 0
    Single-digit minutes must have a leading 0
    Load average may or may not have a decimal point
    If the load average does have a decimal point, there must be at least one digit before the decimal point, and at least one digit after the decimal point

    First regex:

    ^ # start of string
    ( # begin capture of time
    2[0-3]|1d|0d|d # hours
    : # colon
    0-5d # minutes
    (?:s[aApP][mM]) # optional space w/ AM or PM
    ) # end capture of time

    Second regex:
    ( # begin capture of load average
    d+ # mandatory pre-decimal digits
    (?:.d+) # optional literal dot and post-decimal digits
    ) # end capture of load average
    $ # end of string

  • Anonymous
    November 16, 2005
    Let's try that again :) Fixes in caps.

    ^ # start of string
    ( # begin capture of time

    # FIX -- STOPPED RUNAWAY |s
    (?:2[0-3]|1d|0d|d) # hours

    : # colon
    0-5d # minutes

    # FIX - MADE IT OPTIONAL
    (?:s[aApP][mM])? # optional space w/ AM or PM

    ) # end capture of time

    Second regex:
    ( # begin capture of load average
    d+ # mandatory pre-decimal digits

    # FIX -- MADE IT OPTIONAL
    (?:.d+)? # optional literal dot and post-decimal digits

    ) # end capture of load average
    $ # end of string

  • Anonymous
    June 09, 2009
    PingBack from http://greenteafatburner.info/story.php?id=2289

  • Anonymous
    June 09, 2009
    PingBack from http://quickdietsite.info/story.php?id=4005