in reply to index with regex
If you want the position in the string where the match *starts* (rather than where it left off, as pos gives you), you could usepos SCALAR pos Returns the offset of where the last C<m//g> search left off for the variable is in question (C<$_> is used when the variable is not specified). May be modified to change that offset. Such modification will also influence the C<\G> zero-width assertion in regular expressions. See L<perlre> and L<perlop>.
or something. For example:pos() - length $&
Prints:my $R = "foo bar quux baz"; while ($R =~ /ba.\b/g) { print pos($R) - length $&, "\n"; }
4 13
|
---|