Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to print the qr regex to the stdout?

by ikegami (Patriarch)
on Feb 28, 2022 at 17:45 UTC ( [id://11141711]=note: print w/replies, xml ) Need Help??


in reply to How to print the qr regex to the stdout?

Stringification of a compiled regex produces something that can be used in other qr//, m// or s/// without changing the meaning of the pattern. Adding (?^:...) or similar is necessary to achieve that.

my $re = qr/a/; say "a" =~ /$re/ || 0; # 1 say "A" =~ /$re/ || 0; # 0 say "a" =~ /$re/i || 0; # 1 say "A" =~ /$re/i || 0; # 0 my $pat1 = "(?^:a)"; say "a" =~ /$pat1/ || 0; # 1 say "A" =~ /$pat1/ || 0; # 0 say "a" =~ /$pat1/i || 0; # 1 say "A" =~ /$pat1/i || 0; # 0 my $pat2 = "a"; say "a" =~ /$pat2/ || 0; # 1 say "A" =~ /$pat2/ || 0; # 0 say "a" =~ /$pat2/i || 0; # 1 say "A" =~ /$pat2/i || 0; # 1 XXX

That said, you can get the unmodified pattern (and the flags) using re's re::regexp_pattern.

my ( $pat, $flags ) = re::regexp_pattern( $re );

You can rebuild the original compiled regex using the following:

my $re = eval "no re; qr/\$pat/$flags";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-20 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found