in reply to Pattern Matching with REGEXP: Is the match position trackable?

If you want to know where the match began you can use pos($seq) - length($&) inside your while loop.

I also suggest using /H+5+H+/ instead of what you have. The + quantifier means "1 or more" but is easier to read (and type) than {1,} is.

Update: The anonymous reply below does well to point out the performance hit taken by using the $& special variable. A better approach would be to capture the whole regex with parens and then use pos($seq) - length($1) to obtain the position where the match began.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Pattern Matching with REGEXP: Is the match position trackable?
by Anonymous Monk on Sep 18, 2002 at 01:47 UTC
    This does work, but (from perldoc perlvar):
    $&   The string matched by the last successful pattern match (not
         counting any matches hidden within a BLOCK or eval() enclosed by
         the current BLOCK). (Mnemonic: like & in some editors.) This
         variable is read-only and dynamically scoped to the current
         BLOCK.
    
         The use of this variable anywhere in a program imposes a
         considerable performance penalty on all regular expression
         matches. See the BUGS manpage.