Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Help with my regex

by cspctec (Sexton)
on Jul 31, 2013 at 20:29 UTC ( #1047309=perlquestion: print w/ replies, xml ) Need Help??
cspctec has asked for the wisdom of the Perl Monks concerning the following question:

I have the following data:
Controller Status: Optimal NCQ status: Enabled Status of logical device: Optimal Power State: RPM Supported Power State: RPM, Powered off
And I need a regex that will match "Controller Status", "Status of logical device", and "Power State", but not "NCQ Status" or "Supported Power State".

I have my regex set up like this:

my $match = 'status|power'; foreach (@data) { if (/$match/i) { print $_; } }
That is matching everything. I tried something like this as well:
my $match = '[^ncq]status|[^supported]power';
I thought the ^ncq meant do not match.

Comment on Help with my regex
Select or Download Code
Re: Help with my regex
by toolic (Chancellor) on Jul 31, 2013 at 20:44 UTC
Re: Help with my regex
by SuicideJunkie (Priest) on Jul 31, 2013 at 20:54 UTC

    Every line there except for the blank one, does indeed have either "power" or "status" somewhere in it.

    If your set is as small as the example, you might be best off listing all of the options in a long enough form to make them unique. This specifies exactly what should be found before the first colon on the line.

    # Maybe make the values here a sub reference defining what to call whe +n you see the line in question. my %wantedInfoHash = ('Controller Status' => undef, 'Status of logical device' => undef, ...); while ($line = <>) { if ($line =~ /([^:]):/) { # grabbed everything up to the first colon into $1 if (exists $wantedInfoHash{$1}) { # Found something we wanted, so do something with it. } } }
      I'm not sure how to get "Power State" only without also grabbing "Supported Power State" since they both have the same string in them. I just need the shorter one.
        m/^Power State:/

        The ^ is an anchor to 'beginning of line'.

        The match in that code above grabs everything up to the first colon. Which means it will get "Supported Power State" on that line, which will then not exist in the hash of things you want. Later it will get "Power State" on the shorter line, and that will exist in the hash of things you want.

Re: Help with my regex
by Laurent_R (Vicar) on Jul 31, 2013 at 21:51 UTC

    You could also exclude lines that you don't want:

    my $match = 'status|power'; foreach (@data) { next if /NCQ/ or /Supported/; print $_ if /$match/i; }

    Or even simpler:

    foreach (@data) { print unless /NCQ/ or /Supported/ or /^$/; }
Re: Help with my regex
by moritz (Cardinal) on Aug 01, 2013 at 05:38 UTC

    How about just parsing the whole thing, and afterward using those fields you're interested in, and simply ignore the rest?

    use strict; use warnings; use 5.010; my %states; while (<DATA>) { chomp; if (/^ ([^:]+) : \s+ (.*)/x) { $states{$1} = $2; } } use Data::Dumper; print Dumper \%states; __DATA__ Controller Status: Optimal NCQ status: Enabled Status of logical device: Optimal Power State: RPM Supported Power State: RPM, Powered off

    Prints

    $VAR1 = { 'Supported Power State' => 'RPM, Powered off', 'Power State' => 'RPM', 'NCQ status' => 'Enabled', 'Status of logical device' => 'Optimal', 'Controller Status' => 'Optimal' };

    So all the interesting (and some of the uninteresting) data is in %states.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047309]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (18)
As of 2014-07-29 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (225 votes), past polls