Thanks everyone.....    I got 3 suggestions, 2 almost worked, and one (Bruce) worked right out of the box...
 
    # Matt  if (m/^\s*(\w+)\s*=\s*(.+?)\s*$/) {
           almost:  however it matched the condition of "nothing but white space after the ="
           which I wanted to eliminate.
    # Chad  if (   m/^\s*(\w+)\s*=\s*(\".+\*|\w+)\s*$/ ) {
           Missed the quoted string in data such as "this is some data"
    # Bruce if (m/^\s*(\w+)\s*=\s*(.*\S)\s*$/) {
          This one worked like a charm...

Matt Schneider <[EMAIL PROTECTED]> wrote:
Dave,
 
This is untested but you might try something like this:
m/^\s*(.+?)\s*=\s*(.+?)\s*$/; # the "?" cause the ".+" to match the minimum.
$user{$stanza}{$1} = $2 unless $2 eq "";
 
Matthew Schneider
System Admin / Programmer
SKLD Information Services, LLC
303-820-0863
 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dave Blakemore
Sent: Wednesday, February 23, 2005 9:25 AM
To: perl-unix-users@listserv.ActiveState.com
Subject: [Perl-unix-users] requesting regular expresion help

There has to be a better way, but cannot figure it out (see below)
 
The problem lies with the regex:
  m/^\s*(\w+)\s*=\s*(.+)$/
 
which is trying to match lines in a /etc/security/user file in the format:
 
item = data
 
The /^\s*(\w+)\s*=\s* part works fine being that "item" if well defined as being a single word.
 
It is the (.+)$ bit that is bothering me, and because I could not come up with an adequate
solution I had to add the following two lines to get the thing to work.
      $item = $1; $data = "" $data =~ s/\s*$//;
      $user{$stanza}{$item} = $data unless $data eq "";
Instead of just being able to do:
      $user{$stanza}{$1} = $2;
The problem is:  "data" can consist of and needs to match or notmatch any of the following:
 
do not match these
1)   a null  as in nothing whatsoever following the "=" ignore this one don't want it
2)  any amount of white space following the "=" again ignore this one don;t want it
 
match these but trim any trailing space
1)  a quoted string "something like this"
2)  a single word   
3) either of the above but with trailing white space that would need to be trimed before
    being asigned to the hash.      such as /"something like this"   /
 
My current solution works.... but those two additional lines seem ugly, is there a way I can do it all from within the regex?
 
< --- code ---- >
while ( <USER> ) {
   next if (/^\s*(\*|$)/);
   if(  m/^(\S.*):/ ){
      $stanza = $1;
      next;
   }
   if (m/^\s*(\w+)\s*=\s*(.+)$/) {
      $item = $1; $data = "" $data =~ s/\s*$//;
      $user{$stanza}{$item} = $data unless $data eq "";
   }
}
foreach $stanza (sort keys %user) {
   foreach $item (sort keys %{$user{$stanza}}) {
   print "$stanza: $item = [${%{$user{$stanza}}}{$item}]\n";
   }
}
< --- code ---- >
 
--
db


_______________________
Dave Blakemore
[EMAIL PROTECTED]


_______________________
Dave Blakemore
[EMAIL PROTECTED]
_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to