Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: 'g' flag w/ 'qr'

by Athanasius (Archbishop)
on May 29, 2016 at 03:05 UTC ( [id://1164427]=note: print w/replies, xml ) Need Help??


in reply to 'g' flag w/'qr'

Hello perl-diddler,

If you look at the documentation for qr//, you’ll see that the /g modifier is not supported:

qr/STRING/msixpodualn
perlop#Regexp-Quote-Like-Operators

Which makes sense: qr turns STRING into a regular expression, which may then be used in any number of m{...} and s{...}{...} constructs. The appropriate place to add a /g modifier is at the point of use:

use strict; use warnings; use P; my $re = qr{ (\w+) }x; my $dat = "Just another cats meow"; my @matches = $dat =~ /$re/g; P "#matches=%s, matches=%s", scalar(@matches), \@matches; exit scalar(@matches);

Output:

12:53 >perl 1645_SoPW.pl #matches=4, matches=["Just", "another", "cats", "meow"] 12:54 >

Update:

P.S. - I also just noticed that in addition to stripping out the 'g' option, the 'x' option doesn't seem to work in the regex's parens, i.e. - (?x).

I don’t understand what you’re saying here. Can you give some example code?

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: 'g' flag w/ 'qr'
by perl-diddler (Chaplain) on May 29, 2016 at 07:56 UTC
    In regards to your question: " P.S. - I also just noticed that in addition to stripping out the 'g' option, the 'x' option doesn't seem to work in the regex's parens, i.e. - (?x).

    I don’t understand what you’re saying here. Can you give some example code?"

    Please see my response to the 1st response above. Using the original data from the original question, (?x) doesn't parse the same as when it is used as a suffix. It misses the 1st match, which at the least, is counter-intuitive, if not broken, no?

Log In?
Username:
Password:

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

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

    No recent polls found