How can I find position of matched regex of a single string in Perl? -
this question has answer here:
let's my $string = "xxxxxtpxxxxtpxxxxtp";
if want match: $string =~ /tp/;
multiple times , return position each, how so?
i have tried$-[0]
, $-[1]
, $-[2]
position $-[0]
.
edit: have tried global modifier //g
, still not work.
$-[1]
position of text captured first capture. pattern has no captures.
by calling //g
in scalar context, next match found, allowing grab position of match. until you've found matches.
while ($string =~ /tp/g) { $-[0]; }
of course, store them in variable.
my @positions; while ($string =~ /tp/g) { push @positions, $-[0]; }
Comments
Post a Comment