Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: regex compilation

by davido (Cardinal)
on Feb 12, 2019 at 17:57 UTC ( [id://1229827]=note: print w/replies, xml ) Need Help??


in reply to regex compilation

/$regex2/ will have the same interpolation as qr/$regex2/, so we can look more closely at what you are getting by examining the resulting regex2 object:

my $regex1 = qr/(?:(?|(?:\")([^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))/; my $regex2 = qq/(?:(?|(?:\")([^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))/; print $regex1, "\n", qr/$regex2/, "\n";

The output:

(?^:(?:(?|(?:\")([^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))) (?^:(?:(?|(?:")([^\"]*(?:\.[^\"]*)*)(?:"))))

The single quoted construct is gobbling the escapes.


Dave

Replies are listed 'Best First'.
Re^2: regex compilation
by AnomalousMonk (Archbishop) on Feb 12, 2019 at 20:29 UTC

    FWIW, single-quoted regex notation gets you closer (update: than double-quoting) to the desired final form, but still not all the way and there are some pitfalls. Better, IMHO, to stick to qr//. (BTW: morgon: It's not necessary to escape a  " (double-quote) in a qr// expression.)

    File rx_qr_qq_q_1.pl:

    use warnings; use strict; my $rx_qr = qr/(?:(?|(?:\")([^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))/; my $rx_qq = qq/(?:(?|(?:\")([^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))/; my $rx__q = q/(?:(?|(?:\")([^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))/; print qq{raw qr: $rx_qr \n}; print qq{qr/qq/: }, qr/$rx_qq/, "\n"; print qq{qr/q/ : }, qr/$rx__q/, "\n";
    Output:
    c:\@Work\Perl\monks\morgon>perl rx_qr_qq_q_1.pl raw qr: (?-xism:(?:(?|(?:\")([^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))) qr/qq/: (?-xism:(?:(?|(?:")([^\"]*(?:\.[^\"]*)*)(?:")))) qr/q/ : (?-xism:(?:(?|(?:\")([^\\"]*(?:\.[^\\"]*)*)(?:\"))))


    Give a man a fish:  <%-{-{-{-<

Re^2: regex compilation
by morgon (Priest) on Feb 12, 2019 at 18:05 UTC
    Neat trick, thanks a lot.

    So I need to double all the backslashes.

      So I need to double all the backslashes.

      I would say that you need to use  qr// to build Regexp objects. These objects behave essentially like strings in the situations in which you seem to be interested in using them.


      Give a man a fish:  <%-{-{-{-<

      So I need to double all the backslashes.
      Probably not. I think that you need to use qr//

Log In?
Username:
Password:

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

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

    No recent polls found