[Chicago-talk] Regular expression

Steven Lembark lembark at wrkhors.com
Thu Feb 7 12:03:38 PST 2008


Young, Darren wrote:
> Need some help on a regular expression... I've always been horrible at
> regex's with spaces. Have the output from a Windows cluster command that
> looks like this:
>
> Group                Node            Status
> -------------------- --------------- ------
> Cluster Group        GSBDHCL2        Online  <- the line I'm splitting on.

If you know that the status and node columns
do not have space you could also just strip
off the last two items, whatever's left is
the group.

    # the split with capturing regex returns:
    #
    # (
    #     'Cluster',
    #     ' ',
    #     'Group',
    #     '        ',       -4  discard
    #     'GSBDHCL2',       -3  node name
    #     '        ',       -2  discard
    #     Online'           -1  status
    # )
    #
    # splice off the last four items and whatever's
    # left has the original whitespace embedded in
    # it. join that together with nothing and you
    # have the original group back.
    #
    # this doesn't fry you if the manufacturer
    # botches the dashes (happend to me before);
    # does fry you if the node or status are
    # missing entirely (seems unlikley).

    my $line    = read_the_next_line;

    my @tokenz  = split m{ (\s+) }x, $line;

    my ( undef, $node, undef, $status )
    = splice @tokenz, -4;

    my $group   = join '', @tokenz;


That or you could strip them off with a
search using a regex for space followed
by nonspace:

    my $group   = read_the_next_line;

    $group =~ s{ \s+ (\S+) $}{}x
    or die "No tokens in the group: '$group'";

    my $status  = $1;

    $group =~ s{ \s+ (\S+) $}{}x
    or die "Gak, you have only one token on the group: '$group $status'";

    my $node    = $1;

    # at this point whatever's left in $group
    # IS the group.

enjoi

-- 
Steven Lembark                                          +1 888 359 3508
Workhorse Computing                                       85-09 90th St
lembark at wrkhors.com                                 Woodhaven, NY 11421


More information about the Chicago-talk mailing list