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 (11)

  1. 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?

  2. 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

  3. My Required Name says:

    string x = s.Substring(0, s.IndexOf(" ")) + " / " + s.Substring(s.LastIndexOf(" "));

  4. Vicky Sherrouse says:

    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();

  5. Anonymous says:

    if the format is fixed,

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

    ?

  6. Brian Scott says:

    private static Regex logParser =

    new Regex(@"^(?<time>d*:d*s(?: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;

    }

  7. In Javascript syntax:

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

  8. 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

  9. 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

Skip to main content