Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: regex compilation

by davido (Cardinal)
on Feb 12, 2019 at 17:57 UTC ( #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.


Replies are listed 'Best First'.
Re^2: regex compilation
by AnomalousMonk (Chancellor) 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.)


    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";
    c:\@Work\Perl\monks\morgon>perl 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?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2019-05-23 02:40 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (142 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!