http://qs1969.pair.com?node_id=11139922


in reply to printing LAST_MATCH_START/@- LAST_MATCH_END/@+ array where regex match begin/end

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11139917 use warnings; while( <DATA> ) { my @pos; push @pos, @-, @+ while /\t+/g; @pos or @pos = (-1) x 2; print "@pos\n"; } __DATA__ foo bar baz foo bar foo baz ccc ddd foo bar foo baz ccc ddd foo bar baz foo bar foo baz ccc ddd foo
  • Comment on Re: printing LAST_MATCH_START/@- LAST_MATCH_END/@+ array where regex match begin/end
  • Download Code

Replies are listed 'Best First'.
Re^2: printing LAST_MATCH_START/@- LAST_MATCH_END/@+ array where regex match begin/end
by perl_boy (Novice) on Dec 29, 2021 at 13:59 UTC
    thank's for the tip. and works fine and get the expected results. good thank's :)))

    However I find that that PerlDoc https://perldoc.perl.org/perlvar#Variables-related-to-regular-expressions
    is a bit misleading when it says that indexes/positions before and after regex match can be found in the LAST_MATCH_START/@- and LAST_MATCH_END/@+ array by printing
    print $+[0], " ",$+[1], " ", $+[2], "\n" ;

    again thank's for the tip :)))

      Makes sense to me...

      'abcdef' =~ /b(.)(.)./ and print "end of entire match ", $+[0], " en +d of first group ",$+[1], " end of second group ", $+[2], "\n";

      outputs:

      end of entire match 5 end of first group 3 end of second group 4

      This is exactly as expected.