in reply to Regex "(un)Knowledge"
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.
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Regex "(un)Knowledge" (loop)
by nofernandes (Beadle) on Jul 15, 2003 at 18:20 UTC | |
by tye (Sage) on Jul 15, 2003 at 18:27 UTC | |
by nofernandes (Beadle) on Jul 15, 2003 at 18:55 UTC | |
by tye (Sage) on Jul 16, 2003 at 04:20 UTC |