my $R = "foo bar quux baz"; while ($R =~ /ba.\b/g) { print pos($R) - length $&, "\n"; }