G'day ovedpo15,

I see others have shown you how to access the captured text.

"I came a cross with the Term::ANSIColor module which can help me color the string without writing the color codes myself."

I wasn't sure if you were asking for help using Term::ANSIColor. In case you were, here's a demo. I do recall that I found the documentation difficult to follow when I first used it. By the way, it's a core module: no need to install it.

#!/usr/bin/env perl use strict; use warnings; use Term::ANSIColor; my $path = '/a/b/c/d'; my $re = qr{^/a/b/([^/]*)/([^\/]*)}; my $demo_fmt = '%-28s : '; printf $demo_fmt, 'Input - plain text from OP'; print "$path\n"; if (my @matches = $path =~ /$re/) { my $debug_path = $path; for (reverse 1 .. $#-) { substr $debug_path, $-[$_], $+[$_] - $-[$_], colored($matches[$_ - 1], 'red'); } printf $demo_fmt, 'Output - "c" & "d" are red'; print "$debug_path\n"; printf $demo_fmt, 'Output - internals of string'; use Data::Dump; dd $debug_path; }

Prints:

Input - plain text from OP : /a/b/c/d Output - "c" & "d" are red : /a/b/c/d Output - internals of string : "/a/b/\e[31mc\e[0m/\e[31md\e[0m"

— Ken


In reply to Re: How to color the regex captured groups? by kcott
in thread How to color the regex captured groups? by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.