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 -rlrandallxIn reply to Highlighting Regex Hits by rlrandallx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |