First, let us do something a bit easier. We'll use the character offset instead of the line number as key to the hash:
Then there are several ways to convert character offsets into line numbers. If none of your patterns spanned lines, then I'd probably update the regex to match newlines separately so I could increment a line number count in the same loop. But /* */ can span lines so I think I'd instead do a merge-sort-ish thing similar to:my %hash; my $re= qr{ ( /\* .*? \*/) | ( \/\/[^\n]*) | " (?: [^"\\]* | \\. )* " | ' (?: [^'\\]* | \\. )* ' | . [^/"']* }xs; while( /$re/g ) { $hash{pos($_)}= $1; }
Except I think there is probably at least one off-by-one error in that code. For example, pos($_) might need to be replaced with something from @- or @+ in one or both of those places.my @nl; while( /\n/g ) { push @nl, pos($_); } my $ln= 1; while( /$re/g ) { $ln++ while $nl[$ln-1] < pos($_); $hash{$ln}= $1; }
I hope it gives you an idea where to start to get what you are looking for.
- tyeIn reply to Re: Regex "(un)Knowledge" (loop)
by tye
in thread Regex "(un)Knowledge"
by nofernandes
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |