[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Is this fun? What's going on?



Bill Jones wrote:
> s{((\d+\.)(?:\d+)+)} {<A HREF="nav$2html">$1</A>}g;
> 
> Why does the first match '$1" get the whole match, and
> '$2' is getting the partial (which I feel is arriving
> from the first (\d+\.)  ?

$1 gets the whole match because your entire pattern
is enclosed in ().  $2 is the pattern enclosed
by the () which begins with the second ( in the
pattern, i.e. (\d+\.).

> However, this new match isn't really picking up the
> complete first match.  IE -
> 
> I have 1.2 and it matches 1.2 and 1. respectively;
> but when I have 1.2.3 - the .3 is lost...

correct.  "1.2.3" has two dots, and your pattern
matches only one.
 
> Where 1.2.3.? = book.chapter.section.keyword.et al...
> 
> What I wanted was -
> 
> $1 = complete match, $2 = book and chapters, etc.

try:

s{((\d+(\.\d+)?)(?:\.\d)*)} {<A HREF="nav$2html">$1</A>}g;

with $_ = '1.2.3', this puts '1.2.3' in $1, and
'1.2' in $2.  you can add more "(\.\d+)?" and
more sets of parens if you want other substrings.
--
Steve Lane <sml@zfx.com>

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe