S, karthik (IE03x) am Donnerstag, 17. November 2005 09.54:
> John and Jeff,
>
> Could you please explain the RE used.
>
> /^User:\s*(.+?)$(?=.*=([^[]+)\[)/sm;
>
> I understood that it looks for 'User:\s'. Then I am not able to figure
> out the meaning of the remaining part of RE.

Hi karthik

Let me try to explain (and maybe expect a better explanation from JWK):

Because of 

$/ = "--\n";

which defines what is considered as record separator, the regex is every time 
applied to a whole data record consisting of 5 lines.

/^User:\s* matches the beginning of the first record line and following space.

(.+?)$

Then catches the user name into $1 (the $ matches the [real] line break).
Now to

(?=.*=([^[]+)\[)

The (?=......) construct is a "zero-width positive look-ahead assertion". It 
matches nothing, just asserts that the construct content is following the 
position of the actual match position.
(It's not 100% clear to me why JWK uses (?=) and not a simple (?:) - possibly 
he takes in account the case that there is no [2] data line)

Now, left is, in (?=),

.*=([^[]+)\[

which I split now:

.*

matches as much chars (including line breaks) as possible until to the...

=

last '=' char. This is found in the [2] data line. Left is now

([^[]+)\[

The () catches what's in it into $2. In the () is

[^[]+

which is [^SOMETHING]+ . This matches one or more (due to +) char NOT (due to 
^) containing SOMETHING. SOMETHING is '[' here. The match stops at

\[

, that is, a literal '[' -  in the first record example: [I:0 T:Str]

The modifiers /m and /s are important for the interpretation of '$' and '.' in 
the regex. This and everything else is explained in detail in

perldoc perlre


===

The regex won't do what you want if there are other lines after the [2] line.


joe

[...]
> > > --
> > > User: honorhr40
> > > Got data from YY23 (md1=23,md2=24):
> > > [0] xxxx=46[I:0 T:Int]
> > > [1] pd=[I:0 T:Str]
> > > [2] cosnm=normal[I:0 T:Str]
> > > --
> > > User: lulu-918
> > > Got data from YY18 (md1=18,md2=19):
> > > [0] xxxx=69[I:0 T:Int]
> > > [1] pd=back.zzz.com[I:0 T:Str]
> > > [2] cosnm=normal[I:0 T:Str]
> > >
> > > ......


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