Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How to color the regex captured groups?

by hippo (Bishop)
on Aug 13, 2022 at 10:38 UTC ( [id://11146138]=note: print w/replies, xml ) Need Help??


in reply to How to color the regex captured groups?

So, what would be the best way

Everyone will have different criteria for "best". Here, for example is the most generic approach to wrapping your capture groups inside an arbitrary string. This is best for teaching you how to achieve such a result in a perfectly general way without necessarily being the best way to achieve the stated objective. :-)

use strict; use warnings; use Test::More tests => 1; my $str = '/a/b/c/d'; my $re = qr#^/a/b/([^/]*)/([^\/]*)#; my $want = "/a/b/\033[1;31mc\033[0m/\033[1;31md\033[0m"; my @caps = $str =~ $re; my $out = '/a/b/' . join '/', map {"\033[1;31m$_\033[0m"} @caps; is $out, $want, "Matched '$want'";

🦛

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146138]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found