emilford has asked for the wisdom of the Perl Monks concerning the following question:
I have a configuration file that I need to parse through. The lines will follow one of, say, three formats:
What I need to be able to do is grab any line that starts with "Managed Node" and store the values on either side of the "=" sign in a hash. Simple enough, but I'd like to double check the regex I came up with to see if anything better out there to catch user formatting...differences (i.e. - "a=b" vs "a = b", etc). I'd like to make this as forgiving as possible. On the left side of the "=" sign, numbers, letters, underscores, and dashes. On the right side, I'd like to limit it to a number of possibilities, set in a "|" delimited global variable.Managed Node XYZ123 = MN Type = Combo Rim = Planner
The second thing I need to be able to do is two fold. If a line matches ABC = XYZ, I need to be able to grab both sides of the "=" sign and, in this case, create a variable called $ABC and set its value to "XYZ". I'm not sure how to go about dynamically creating variables like this, but here is my regex:my $options = 'A|B|C|D'; if ($line =~ /^Managed Node ([a-zA-Z0-9_-])\s*=\s*($options)/i) { my ($node, $value) = ($1, $2); }
or would this be betterif ($line =~ /^(\w*)\s*=\s*(\w*)/);
Thanks for the help.if ($line =~ /%(.*)\s*=\s*(.*)/);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: some regex help
by matija (Priest) on Apr 05, 2004 at 21:52 UTC | |
by emilford (Friar) on Apr 05, 2004 at 21:57 UTC | |
Re: some regex help
by Roy Johnson (Monsignor) on Apr 05, 2004 at 21:47 UTC | |
Re: some regex help
by DamnDirtyApe (Curate) on Apr 05, 2004 at 22:17 UTC | |
by emilford (Friar) on Apr 06, 2004 at 01:04 UTC | |
| |
Re: some regex help
by Elijah (Hermit) on Apr 05, 2004 at 21:59 UTC |
Back to
Seekers of Perl Wisdom