rlrandallx has asked for the wisdom of the Perl Monks concerning the following question:
This is a regex question so forgive me if I should be asking it somewhere else.
Here's my problem: I am searching thru a string/document for a word. If I find it, I wish to print the line no., line and highlight the word (square brackets is ok but I would really like ANSI color.) The issue is when there is more than one hit on the same line. I've read all about global matching but nothing works. Below is my code:
: $line = 'This line has a hit here and a hit there."; $word = 'hit'; $count = 0; while ($line =~ /\b$word\b/gi )#I also tried "/gic" & pos() { $line = "$`".'['."$&".']'."$'"; $count++ } print "$lino $line\n"; : print "$word was found $count times\n";
With color, the one line is "$line = "$`".BLACK \ ON_YELLOW ."$&".RESET."$'"; Anyway, if I replace the 'while' with an 'if' it highlights the first 'hit'. I just can't get the other(s) to highlight. I know it is because I an modifying $line but this structure has worked before so I am trying to find an alternate "sure-fire" way.
Thanks for any help -rlrandallx
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Highlighting Regex Hits
by toolic (Bishop) on May 15, 2010 at 00:56 UTC | |
|
Re: Highlighting Regex Hits
by ikegami (Patriarch) on May 15, 2010 at 01:14 UTC | |
by rlrandallx (Initiate) on May 15, 2010 at 02:42 UTC | |
by ikegami (Patriarch) on May 15, 2010 at 03:17 UTC | |
by rlrandallx (Initiate) on May 15, 2010 at 04:43 UTC | |
|
Re: Highlighting Regex Hits
by JavaFan (Canon) on May 15, 2010 at 15:11 UTC | |
|
Re: Highlighting Regex Hits
by Natanael (Acolyte) on May 15, 2010 at 08:55 UTC | |
by ww (Archbishop) on May 15, 2010 at 15:05 UTC |