> So \w means the first non word character. What about the ~ / before
> the
> (\w? And what does the + sign do? Is $buf a command?

Although I do not claim to be an expert, nor do I play one on TV, I'll
give a try at this one. Here's the command that was given to you:

($acct) = $buf =~ /(\w+)/;

$buf, in this case is the line you want to extract your user from. You
have a file, with lines such as the following:

<[EMAIL PROTECTED]> SIZE=1024

and you extract each of these lines at a time into $buf (after opening
the file of course).

while ($buf = <FILE>) {
    # $buf now contains line of file, one per each loop of while
    $buf =~ /(\w+)/;
    $userName = $1;
    ...do something with $userName
}

Now, to answer the remaining questions. =~ is the 'pattern match'
operator. It's what actually fetches the first word pattern (user).
Likewise, !~ says 'do not match'.

When 'user' is extracted, it is put into special variable $1. This is
what the parenthesis does.

The '+' operator says 'get one or more word characters [A-Za-z0-9_]
until we run out of word characters to capture'. '+' is what is known
as a greedy operator. It will grab things until it can't get any more
(or until it has to save enough data in the $buf variable for further
regex's to match, which is not the case here). This means that it
looks past the first '<', and reads up until the next non-word char,
'@', returning 'user' (w/o quotes).

Read:

perldoc perlop
perldoc perlretut
perldoc perlre

I HTH, and I surely hope that I've been reasonably accurate with my
assessment.

Steve


>
> Thanks
>
> AD
>
> -----Original Message-----
> From: Bob Showalter [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, October 19, 2004 8:57 AM
> To: 'Khairul Azmi'; [EMAIL PROTECTED]
> Subject: RE: simple regular expression problem
>
> Khairul Azmi wrote:
>> Hi all,
>> I am a newbie. I just need to extract the string containing the unix
>> account from the following text
>>
>> <[EMAIL PROTECTED]> SIZE=1024.
>
> I'm guessing you want to extract the string "user"? (But how do you
> know
> that that corresponds to a Unix account?)
>
> The following will catpure "user" from the example you gave:
>
>    ($acct) = $buf =~ /(\w+)/;
>
> This works because:
>
>    a) \w matches 'u', 's', 'e', and 'r', but not '<' or '@'
>    b) regexes match the "longest, leftmost" sequence, so
>       'user' matches instead of 'gmail'.
>
> You would be advised to replace \w with the actual class of characters
> that
> are allowed in user names...
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



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