Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I apply switches like /i or /g to a qr regexp?

( #368332=categorized question: print w/ replies, xml ) Need Help??
Contributed by ambrus on Jun 20, 2004 at 21:53 UTC
Q&A  > regular expressions


Description:

How do I apply switches to a precompiled (qr) regexp.

This code does not work:

$regexp = qr/^(get|post)\b/; "GET / HTTP/1.1"=~m/$regexp/i and print " +match\n";

Answer: How do I apply switches like /i or /g to a qr regexp?
contributed by ambrus

There are two knid of regep switches in perl.

The first kind is the swithes /imsx, which change the meaning of the regexp itself. The second is /gce, which only change what perl does with a match; this kind can only be applied to the whole regexp, not some part of it.

For swiches /imsx, you have to write these directly after the closing delimiter of the qr, because perl has to know about the switches when it compiles the regexp. Thus, to match case-insensitively, you'll need something like this:

$regexp = qr/^(get|post)\b/i; "GET / HTTP/1.1"=~m/$regexp/ and print " +match\n";
As you already know, if you use a single qr scalar in a m// expression, the regexp won't get recompiled. The m// or s/// is neccesarry only if you want to add one of the switches /gce. Even if you have other characters in the m//, the /imsx flags after m// does not apply to the regexp part inside the qr, so the following code won't match either as (get|post) is matched case-sensitively.
$regexp = qr@get|post@; "GET / HTTP/1.1"=~m@^$regexp\s+(\S+)@i and print "match\n";

If you stringify a qr regexp, like print qr/hallo/;, you'll see that it prints like (?-xism:hallo) showing that the flags are fixed in it, even if you embed it in a larger regex.

On the other hand, you cannot use any of the /gce switches in a qr like qr/^count:\s*(\d+)/g, as these make sense only with a m// or s///. Thus, to match a regexp from pos, you need to write

$regexp = qr/(\d+)/; while ($line=~/$regexp/g) { print "number $1 found\n"; }

This question is also answered in Friedl, Mastering Regualr Expressions, chapter 7 (p. 299 and p. 346 in the translation).

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (13)
    As of 2014-08-28 13:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (261 votes), past polls