in reply to Re: Finding the index of a string with a regexp
in thread Finding the index of a string with a regexp

That's what I'm looking for, but isn't there any way to get it to return the offset for the beginning of the match instead of the end?
  • Comment on Re^2: Finding the index of a string with a regexp

Replies are listed 'Best First'.
Re^3: Finding the index of a string with a regexp
by bart (Canon) on Sep 09, 2005 at 23:44 UTC
    Look at the arrays @- and @+, which point to the start resp. the end of submatches — use index 0 for the whole match. So you want $-[0].

    Plus: unlike pos, it works without using /g, too.

    You didn't really think Javascript could do something with regexps that Perl couldn't, did you? Where did you think its implementors got the ideas from, anyway?

Re^3: Finding the index of a string with a regexp
by Hue-Bond (Priest) on Sep 09, 2005 at 23:49 UTC

    The first thing that comes to mind is substracting the length of the match, like in:

    pos ($state) - length $&

    --
    David Serrano