Article 527J2 [SOLVED] Perl: Highlight the text that Matches the Regex

[SOLVED] Perl: Highlight the text that Matches the Regex

by
blueray
from LinuxQuestions.org on (#527J2)
When I use ag or rg to select a text, it highlight the selected text.

Code:$ echo "First HTML appeared, then CSS, then JavaScript" | rg -N 'HTML|php|CSS|Java(Script)?'
$ echo "First HTML appeared, then CSS, then JavaScript" | ag --nonumbers 'HTML|php|CSS|Java(Script)?'But when I use Perl, it does not highlight the matching text.

Code:$ echo "First HTML appeared, then CSS, then JavaScript" | perl -ne '/HTML|php|CSS|Java(Script)?/ and print'I am attaching am image to illustrate my point.

I tried the following:

Code:echo "First HTML appeared, then CSS, then JavaScript" | perl -ne 'use Term::ANSIColor qw(:constants);/HTML|php|CSS|Java(Script)?/ and print RED, $_'

echo "First HTML appeared, then CSS, then JavaScript" | perl -ne 'use Term::ANSIColor;/HTML|php|CSS|Java(Script)?/ and print color("red"), $_'It changes the color of the whole text and not the selected text.

How can I highlight the text that matches the Regex when using Perl to search?
Attached Thumbnailsattachment.php?attachmentid=32998&stc=1& latest?d=yIl2AUoC8zA latest?i=ysXQBHDgezM:fCnxs4aB8so:F7zBnMy latest?i=ysXQBHDgezM:fCnxs4aB8so:V_sGLiP latest?d=qj6IDK7rITs latest?i=ysXQBHDgezM:fCnxs4aB8so:gIN9vFwysXQBHDgezM
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments