Richard Lee wrote:
John W. Krahn wrote:
Richard Lee wrote:
While reading 'mastering perl', I run into @- and @+ for the first time.

perldoc perlvar


Trying to understand what's going on, I ran the code from the book, but

$-[1] and $+[1] shoudln't match only the first match? (in this case,
shoudln't it be, 7 to 8 ?, instead of 7 to 9 ?)

The length of the first match is 2. 8 - 7 = 1. If $+[1] had the value 8 then the first match would only have a length of 1.

Look at the @LAST_MATCH_START entry in:

perldoc perlvar


Also read the entries for $<digits>, $MATCH, $PREMATCH, $POSTMATCH, $LAST_PAREN_MATCH, $^N, @LAST_MATCH_END and $LAST_REGEXP_CODE_RESULT.

And of course the regular expression documentation:

perldoc perlrequick
perldoc perlretut
perldoc perlre
perldoc perlop

Also see:

perldoc -f pos
perldoc -f study


According to the doc,

since $1 is  substr($var, $-[1], $+[1] - $-[1] )

and hi matches 7 and 8th elements of the variable and length has to be 2, so substr($var, 7, 2), but I still don't understand because if formula was applied directly, it still would be, substr($var, 7, (8-7)) which would come out to substr($var, 7, 1). so, how I am reading it is then, $+[1] is not a value that I read off the variable but something perl assigns it to it the way it see fit?(in order to match the first match $1) and since lenght of the match is 2, $-[1] is fixed(where the match starts) but $+[1] tries to match the length?

@+ definition does mentions $+[1] is hte offset past where $1 ends.

Say you have the string "abcdefghi".

The positions in the string are:

  a b c d e f g h i
 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
 0 1 2 3 4 5 6 7 8 9

If you have the regular expression:

/(de)/

Then the match starts at position 3, moves forward two characters, and ends at position 5, where the next match, if any, will start.

If it stopped at position 4 then it would only match 1 character.

If you want to explore all the gory details of regular expressions then get the book _Mastering Regular Expression_ by Jeffrey E. F. Friedl:

http://www.oreilly.com/catalog/regex3/index.html



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to