Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to print the qr regex to the stdout?

by ovedpo15 (Pilgrim)
on Feb 28, 2022 at 09:49 UTC ( [id://11141699]=perlquestion: print w/replies, xml ) Need Help??

ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
Really simple question here but can't seem to make it work. As part of a debugging message, I'm trying to print the regex operator which is saved in a var. If I use Data::Dumper, the var looks like (it's a hash where one key is regex):
{ 'regex' => qr/^\/a\/.*?\/b\/.*\/pkgs\/([^\/]*)?\/([^\/]*)?\/.*/ }
When I print the message:
print("Regex: $regex\n");
I get:
Regex: ?^:^/nfs/.*?/itools/.*/pkgs/([^\/]*)?/([^\/]*)?/.*/)
It adds the ?^: at the start. What is the proper way to get rid of it?

Replies are listed 'Best First'.
Re: How to print the qr regex to the stdout?
by ikegami (Patriarch) on Feb 28, 2022 at 17:45 UTC

    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";
Re: How to print the original qr regex to the stdout?
by Anonymous Monk on Feb 28, 2022 at 10:15 UTC
      Thank you very much!
      One question - why the second line is needed? It looks like $pat does what I need.
        My cell phone doesnt have perl, the doc example implied the (^:) being returned
Re: How to print the qr regex to the stdout?
by LanX (Saint) on Feb 28, 2022 at 12:12 UTC
    On another note:

    You used a lot of escaping of \/ in your regex:

    > "regex" => qr/^\/a\/.*?\/b\/.*\/pkgs\/([^\/]*)?\/([^\/]*)?\/.*/

    Are you aware that qr and his siblings of "quote-like-operators" allow other delimiters?

    Like qr(...) or qr~...~ and so on?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11141699]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (None)
    As of 2024-04-25 04:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found