http://www.perlmonks.org?node_id=1047313


in reply to Help with my regex

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

Replies are listed 'Best First'.
Re^2: Help with my regex
by cspctec (Sexton) on Jul 31, 2013 at 21:13 UTC
    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.