G'day perl-diddler,

"So how can I attach the "/g" modifier to my "qr" regex ... ?"
  1. Short answer: you can't so stop trying.
  2. Longer answer: read on ...

The 'g' modifier is used by m// and s/// to direct how a regex is to be used (single match, global substitution, etc.); it does not affect the regex itself. qr// has no 'g' modifier. Here's links to all three (note the modifier lists):

The 'g' modifier is not part of qr//'s syntax and, if used, syntax errors are raised (as expected).

$ perl -wE 'my $re = qr{A}g' Unknown regexp modifier "/g" at -e line 1, near "= " Execution of -e aborted due to compilation errors.

You also can't do it with the re pragma's '/flags' mode:

$ perl -wE 'use re "/g"' Unknown regular expression flag "g" at -e line 1.

See also:

On a side note — related to what you're doing but not the current problem at hand — are you familiar with the '(?<flags>:<pattern>)' regex construct described in perlre: Extended Patterns: (?adluimnsx-imnsx:pattern)? This construct, and qr//'s interpolating, allows you to write something like this:

$ perl -wE 'my ($p, $f) = (A => "x"); my $re = qr{(?$f: $p )}; say $re +' (?^u:(?x: A ))

Now you control the available flags and don't have to worry about qr//'s modifiers.

By the way, you can't add a 'g' modifier using this method either.  :-)

$ perl -wE 'qr{(?g:)}' Useless (?g) - use /g modifier in regex; marked by <-- HERE in m/(?g < +-- HERE :)/ at -e line 1.

— Ken


In reply to Re: 'g' flag w/'qr' by kcott
in thread 'g' flag w/'qr' by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.