I am reading a complete file in a string and then doing a regex match as below:
if($str =~ m/$regex/gc) {
$offset = $+[0];
}
Using this code, I can capture the position where the last successful match ends.
Now this will give the position as character number.
Is there any way that I can get offset as line number?
What I am doing for now is that I am counting the number of newline characters from beginning of $str upto end $offset.
I want to know is there a direct way to capture line number for a regex match.
Contrary to what one might imagine, Nahuel’s suggestion of using
$.is actually doable in this case.This is because one can read from strings just like files using Perl:
OUTPUT