web analytics

Perl: how to get the position of the match

Let’s search for the word “brave” in the string “Hello, new brave world!”. There are several methods to get the position of the match in Perl.

#1 – use POS function Function pos returns the position, where search was finished.

$txt = "Hello, brave new world!" ; $txt =~ m/(brave)/g; my $pos = pos($txt) – […]