On Tue, 2006-04-18 at 11:20 -0600, Ed wrote:
> How would I get the various elements out of a string of known format.
> A previous post about a similar topic approaches a solution but I need
> help with the regular expression to pass to the m function.
> 
> Here's an example of a string:
> net user cknotts somepassword /add /active:yes /expires:never
> /comment:"Some Comment" /fullname:"Cindy Knottsagain"
> 
> The code would look something like this, except the regex in the m{}
> function needs work:
> 
> while (my $line = <F>)
> {
>      if( my( $UserName, $passwd, $Comment, $FullName ) = ( $line =~
> m{^\s*net\+user\s+(\S+)\s+(\S+)\s*} ) )
>      {
>           print $UserName, $passwd, $Comment, $FullName;
>      }
> 
> }
> Thanks,
> 

See perldoc perlre.

You're attempting to assign values from a regular expression match to
variables. However, the syntax you're using seems that it is going to
assign the number of matches to the $UserName variable, and the rest are
going to be undef; Try something like:

while (my $line = <F>)
{
     my( $UserName, $passwd, $Comment, $FullName );
     ($UserName, $passwd, $Comment, $FullName) = ($1,$2,$3,$4) if
( $line =~ m{^\s*net\s+user\s+(\S+)\s+(\S
+).*/comment:"(.*?)".*/fullname:"(.*?)"} ) )
     {
          print $UserName, $passwd, $Comment, $FullName;
     }

}

This will totally break if the command line options to the 'net use'
command are specified in a different order. To handle that you'll need a
much more complex regex. "Mastering Regular Expressions" is a fine book
on the subject.

HTH

-- 
Joshua Colson <[EMAIL PROTECTED]>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to