>> I have a config file that can have three formats:
>> "[alpha] [alpha]", "[alpha]: [alpha]" or "[alpha] *[alpha]"

> Something like: 

/\s*([\w-_\.]+)\s*[ \:\*]\s*([\w-_\.]+)/

> would load your alphas into $1 and $2 regardless of the format of the 
line.

In general, it a good idea to check your matching anyway, as in (don't 
need prefixing \s* if you're not capturing it):
if ( $line =~ /([\w-_\.]+)\s*[ \:\*]\s*([\w-_\.]+)/  ) { 
   # got a match
   print "$1  is $2\n";
} else {
   print STDERR "Bad config line: $line\n";
}

dashes in char class can mean ranges, as in:
[0-9] or [a-z]

and it often happens that folks forget that and do:
[%$#-&]

and end up allowing all the chars from '#' to '&'.  Either escape the dash 
or put it at the beginning or end of your char class. Yes, I'm betting 
that perl, seeing the dash after a '\w' won't try and make it a range but 
... if somebody adds another char at a later point, like a comma:
[\w,-_]

Your original post had the colon 'left justified' and the star right 
justified, which makes it sound like 'zero or one colons, at least one 
space and zero or one stars'
if ( $line =~ /\s*([-\w_.]+):?\s+\*?([-\w_.]+)/  ) { 

by putting them in capturing parens, you could check to see which matched:
if ( $line =~ /\s*([-\w_.]+):?\s+\*?([-\w_.]+)/  ) { 
    my ($tag, $sep, $value) = ($1, $2, $3);
 # got a match
 print "$tag is $value\n";
    if ( $sep =~ /:/ ) {
    } elsif ( $sep =~ /\*/ ) {
       print "In colon format\n";
    } else
       print "In space format\n";
   }
 
if that's important.  There's also split:
my ($tag, $value) = split(/[\s:*]+/, $line);

(or, if you need the separators:
my ($tag, $sep, $value) = split(/([\s:*]+)/, $line);
 
) but that doesn't give you any syntax/formedness testing, it does, 
however, let you smoosh right into a hash:
my %config = map {split(/[\s:*]+/) }  <>;

not that that's a good idea ... validating syntax and well-formedness is a 
very good idea for config files.

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

"Outside of a dog, a book is man's best friend. Inside of a dog, its too 
dark to read."
Groucho Marx
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to