perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
I especially liked methods 2 & 3 as they didn't need interpolation, with 3 being better as it didn't require an intermediate variable.sub iregex { qr/ ... /ix } 1) my $regex = iregex() if ($isbn =~ m/$regex/ ) { print "Matched!\n } 2) my $regex = iregex() if ($isbn =~ $regex ) { print "Matched!\n } 3) if ($isbn =~ iregex() ) { print "Matched!\n }
Seeing that I wanted to try out capturing all matching sub expressions using 'g' added to my 'qr' expression, but I'm getting a deprecation error:
> perl -we'use strict;use P; my $re = qr{ (\w+) }gx; my $dat = "Just another cats meow"; my @matches = $dat =~ $re; P "#matches=%s, matches=%s", scalar(@matches), \@matches; exit scalar(@matches);' output: Having no space between pattern and following word is deprecated at -e + line 2. Bareword found where operator expected at -e line 2, near "qr/ (\w+) / +gx" syntax error at -e line 2, near "qr{ (\w+) }gx" Execution of -e aborted due to compilation errors.
If I move the 'g' option down to where the RE is used and use a *second* "RE" operator (i.e.: /.../g) and interpolate my expression into another RE (the slowest of the above 3 options), I get:
> perl -we'use strict;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: #matches=4, matches=["Just", "another", "cats", "meow"]
So why can't I add the global switch to the "qr" expression? FWIW, I also tried qr{ (?xg) }. Perl strips out the 'g' and tells me to add it to the end of the RE -- where it is deprecated:
perl -we'use strict;use P; my $re = qr{ (?xg) (\w+) }; my $dat = "Just another cats meow"; my @matches = $dat =~ $re; P "#matches=%s, matches=%s", scalar(@matches), \@matches; exit scalar(@matches);' output: Useless (?g) - use /g modifier in regex; marked by <-- HERE in m/ (?xg + <-- HERE ) (\w+) / at -e line 2. #matches=1, matches=["another"]
So how can I attach the "/g" modifier to my "qr" regex so I can use the direct match as in #2 or #3 above?
Thanks...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).
|
|---|
| Replies are listed 'Best First'. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Re: 'g' flag w/'qr'
by AnomalousMonk (Archbishop) on May 29, 2016 at 02:59 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The /g modifier can simply not be used with qr// to build a regex object. It can only be used with the m// and s/// operators; interpolation of a regex object into one of these (or literals) is your only hope. Please see the discussion of qr// in Regexp Quote-Like Operators in perlop and note the available modifiers. See also the discussions of m// and s/// (and their available modifiers) in the same section.
Update: ... I also just noticed that ... the 'x' option doesn't seem to work ...It works just fine:
Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by perl-diddler (Chaplain) on May 29, 2016 at 07:52 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
It implies that the decision was somewhat arbitrary and that it could still work the old way, except someone chose to add another *wart* to perl and create another exception -- that RE's work a certain way in some places, but another way when described by 'qr'. As for 'x' working -- i'd call this broken. Why not use the example I provided that shows the broken behavior rather than coming up with some different example where you could make it work? I.e.:
the above doesn't work. It doesn't return the 1st match from the text line. As opposed to moving the (?x) to the end of the qr statement:
In the case where I am not using 'g', One would expect the re to match and return the 1st word in the list. With (?x), it doesn't return the 1st word, but returns the 2nd, vs. with 'x' as a suffix, it behaves as expected and only returns the 1st matching word. Then you claim the status quo makes sense, when the perl mastery book clearly shows that 'qr' can stand as an RE by itself without 'm' or 's' -- AND it is more efficient when it is used that way. Except it has been crippled by differences in how 'RE's work in 'qr' vs. ones that are "requoted", re-interpolated, and re-compiled in 'm' & 's'. Why is there a difference when 'qr' can be used "standalone"? "qr" wasn't meant to replace 's', but it was meant to replace or be equivalent to 'm'. Did you see the 3 ways Mastering perl uses 'qr'. The first way, where it is interpolated, seems to have little benefit over using q(string). then interpolating the string into the m{} statement. But if you can use 'qr' w/o the m{}, you only do the RE-compile once when you use 'qr' -- which can be used to match things directly in "=~" statements -- just like m{} statements are used. So again, I ask 'why' the artificial constraint when, from the deprecation warning, it seems apparent, that it used to work. | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by Athanasius (Cardinal) on May 29, 2016 at 09:29 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I’m guessing you’re running a fairly old version of Perl? On 5.20.2, I get:
— which is clear. But on 5.12.3, I get:
— which isn’t. In any case, the “deprecated” message you’re seeing is related only indirectly to the presence of a /g modifier on a qr// term. The Perl compiler is simply confused as to what the syntax is supposed to mean, and its (incorrect) guess leads it to find a construct which you never intended and which happens to be deprecated. Actually, the qr// syntax was introduced in Perl 5.005,1 which was released in 1998. The first edition of the Camel Book to document qr// was the third edition, published in July, 2000 (4 months after the release of Perl 5.6). The section “Pattern-Matching Operators” in Chapter 5 draws a distinction between those modifiers which apply to a regex (are are therefore applicable to qr//) and those which apply to an operator (and are therefore applicable only to m// and s///).2 The /g modifier falls in the second category. So, AFAICT, putting a /g modifier on a qr// term is not “deprecated,” as it was never allowed in the first place. 1Update (May 30, 2016): See perl5005delta#New qr// operator. Hope that helps,
| [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by Corion (Patriarch) on May 29, 2016 at 08:05 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
You're switching on ?x too late in the regular expression:
The first whitespace is not governed by ?x. | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by perl-diddler (Chaplain) on May 29, 2016 at 12:38 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by Anonymous Monk on May 29, 2016 at 08:02 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
how now brown cow did you try splain?
Do you get it? That is in addition to Bareword found where operator expected None of that means qr//g ever worked or was meant to work, the g was always ignored by qr | [reply] [d/l] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Re: 'g' flag w/ 'qr'
by Athanasius (Cardinal) on May 29, 2016 at 03:05 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Hello perl-diddler, If you look at the documentation for qr//, you’ll see that the /g modifier is not supported: qr/STRING/msixpodualn 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:
Output:
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,
| [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by perl-diddler (Chaplain) on May 29, 2016 at 07:56 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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? | [reply] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Re: 'g' flag w/'qr'
by kcott (Archbishop) on May 29, 2016 at 09:53 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
G'day perl-diddler, "So how can I attach the "/g" modifier to my "qr" regex ... ?" 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).
You also can't do it with the re pragma's '/flags' mode:
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:
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. :-)
— Ken | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by perl-diddler (Chaplain) on May 30, 2016 at 01:25 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Yes, but I seem to be finding "holes" in my memory...i.e. I only remembered the m{ (?i) <pattern> } form... I take it that the ":<pattern>" part allows the flags to apply only to the pattern after the colon and before the end-of-parens... I readily admit not to knowing how to use every feature in perl's "RE's"... some of which I might use in some odd case, but many of which I put in my "tmp" memory because they are experimental. Too many features have been "experimental" in perl for too long, and it really gets confusing -- since my conception of "experimental" was something that was being introduced but might not be stable yet -- however -- when something that was intro'd as Xperimental, but then was stable for more than 2 major releases, that really doesn't fall into the category of experimental, but more of of "some developer's personal 'pet feature'", that got introduced, but was never removed when the experiment was "over". Some of those features were introduced to fill in "holes" in perl (case statement). The "Switch" module that was part of core before 5.8, was deprecated with the introduction of "given/when" and its documentation was changed recommending it's usage. Trouble is, you had a Core module deprecated that in the deprecation notes told you to use "given/when" instead -- when, w/5.18, many years after 5.8, anything that never had the experimental label removed, generate sometimes fatal diagnostics (if you follow advice in most languages to get rid of all warnings, and then make all warnings "fatal"). I tended to think that advice applied to computer languages and good-programming practices, in general -- but 5.18 made it clear that those rules didn't apply to perl. ;^/ (*sigh*) | [reply] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by AnomalousMonk (Archbishop) on May 30, 2016 at 03:05 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
... Extended Patterns: (?adluimnsx-imnsx:pattern) ... This is the very useful and highly non-experimental "non-capturing group" construct. It is most commonly found in its modest (?:pattern) form, with no modifiers present. If you haven't already, I suggest you seek the company of this potentially stalwart and faithful regex companion. Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Re: 'g' flag with 'qr' (different commands) (updated)
by LanX (Saint) on May 29, 2016 at 12:23 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The /g variants are for repetitive matches / looping ! It's comparable to switching between if and while , which are obviously different beasts.
If you look at other languages ( I wished the perldocs were clearer here because it's causing confusion) Furthermore are regular expressions meant to be combinable/nestable , but a sub expression with /g doesn't make any sense.
Cheers Rolf
¹) couldn't reproduce this easily, JS is very close to Perl in many aspects, I had probably another language in mind... Python, PHP? | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by perl-diddler (Chaplain) on May 30, 2016 at 01:47 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I was noting the similarity between these forms: In both of the above, "@matches" will get 2 entries "re" and "xp". I find it unfortunate that if your regexp was doubled, there is no way to directly use qr w/'g' i.e. one must use the m{} or // type form as in:
| [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by AnomalousMonk (Archbishop) on May 30, 2016 at 04:01 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I find it unfortunate ... Sometimes life's like that.
So far, you've only advocated the utility and desirability of code like I think you need to consider the implications of the cases raised by haukex here. These (and I'm sure many other) cases would need to be given defined behaviors. Surely, you do not intend the qr//g feature to apply only to the $string =~ $regex case? If so, this feature would seem to fall squarely into the "some developer's personal 'pet feature'" category. Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by LanX (Saint) on May 30, 2016 at 11:13 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I don't.
Cheers Rolf
| [reply] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Re: 'g' flag w/'qr'
by haukex (Archbishop) on May 29, 2016 at 11:12 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Hi perl-diddler, So why can't I add the global switch to the "qr" expression? Here are some of my thoughts about the "why" part of the question: the way I like to think of this is that /g doesn't modify the regular expression (the inside of the /.../ construct), it modifies how m// and s/// work. A thorough reading of Regexp Quote Like Operators shows that this behavior is pretty complex, and for example the return values of m// depend on context, whether or not the regular expression contains capturing groups, and whether the /g modifier is present. Consider that when you construct a regular expression with qr//, it is not yet known what kind of regular expression operator it will be used in - it can be used in both an m// and an s///. Lastly, consider this hypothetical situation:
Hope this helps, | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Re: 'g' flag w/'qr'
by ikegami (Patriarch) on May 30, 2016 at 04:52 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The g flag tells the match operator and the substitution operator to match repeatedly. It makes no sense to use on other operators that do not performing any matching (such as q, qq, qr, qx and tr).
The options that pertain to the meaning of regular expression pattern are documented in perlre. The others are documented as part of the documentation of the operators to which they pertain. | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by perl-diddler (Chaplain) on May 31, 2016 at 21:25 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'perlop' says (*emphasis mine*): Going by: It must be the case that both m{} and qr{} are *both* pattern matches. Isn't a "pattern match" a "regular expression pattern"? The fact that the =~ treats them the same, but the 'g' flag only works on one of them seems counter-intuitive. I realize that "behind the scenes", documentation says they "don't", but why, might not, the 'g' flag apply to sub-pattern, so, at least, things like "\G" would work inside "qr"? (Note, \G is is defined as a legal zero width assertion that appears "usable" in a "qr" pattern (but I'm not sure to what effect w/o "(?g)). After more than a bit of experimenting, I found '\G' is usable, but a bit awkward to use inside 'qr' op, since it only will work when wrapped with an 'm{}', though even there, for some reason, it returns an extra pair of matches that contain undef:
ARG!!!... I'm getting a headache.
p.s. -- how does one get unicode characters to display in '<code>'? UPDATE: removed/replaced version of code that used the default undef (∄) symbol to use "(undef)" instead. | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by ikegami (Patriarch) on Jun 01, 2016 at 00:17 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
No. Read qr's documentation. It does not perform any matching. It simply compiles a pattern.
Matching is the action of checking if something is consistent with a definition. A pattern is a definition of a set of strings.
Indeed it does. These matching operators are m//, s/// and tr///. If you don't use one of these explicitly, you are using m// implicitly.
This means that all of the following are functionally equivalent:
What it doesn't mean is that all operators perform regex pattern matching. qr/abc/g makes no more sense than qq/abc/g. | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by AnomalousMonk (Archbishop) on Jun 01, 2016 at 01:14 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
... I found '\G' is usable, but ... it returns an extra pair of matches that contain undef ... This has nothing to do with the \G assertion, but is a facet of the way unmatched capture groups behave in list context when allowed to match zero times. Consider: Both of the variations above, with and without the \G assertion q((?:\G(\w)\W{2}(\w))*) and q((?:(\w)\W{2}(\w))*) but with a * quantifier on the (?:...) group containing the capture groups, produce pairs of spurious undef values, although the other values generated are different. Versions of the regex eliminating the * quantifier (or using a + quantifier, but no example of this is given) do not produce spurious undefs: Don'cha just love regexes? Play with variations of these patterns (including qr[$qr_string*] and qr[$qr_string+]) for deeper confu... um, greater enlightenment. So what's going on? Here's how I would describe it: If the (?:...(...)...(...)) group containing two capture groups is allowed to match zero times at some point, e.g., the end of the string, it will! However, the capture groups inside it don't actually capture anything, so they return undef. Compare that behavior to unmatched capture groups in an alternation:
Also consider:
Update: In place of the last example, consider instead: For discussion of $-[0], please see @- in perlvar. Also note that the definition my $qr = qr[$qr_string]; was changed from the previous example to remove the + quantifier, which was included accidentally and only served to obscure the example. Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by LanX (Saint) on May 31, 2016 at 21:53 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> Binary "=~" binds a * scalar expression * to a * pattern match * . perlop also says
If the right argument is an expression rather than a search pattern, substitution, or transliteration, it is interpreted as a search pattern at run time. this means there is a DWIM behaviour to fall back to match m// and the following are equivalent:
please note that you could also use a plain string (line 101), but still without /g. ِAGAIN /g transforms m// and s/// to different commands with different contextual behaviour! for instance The "/g" modifier specifies global pattern matching--that is, matching as many times as possible within the string. How it behaves depends on the context. In list context, it returns a list of the substrings matched by any capturing parentheses in the regular expression. If there are no parentheses, it returns a list of all the matched strings, as if there were parentheses around the whole pattern.
Cheers Rolf
| [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Re: 'g' flag w/'qr'
by Don Coyote (Hermit) on May 29, 2016 at 11:34 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Hello perl-diddler Keep reading... Global Matching Anchors section, beginning on page 5, lighteth a way for Seekers of Perl Wisdom. Ahh... the catacombs | [reply] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Re: 'g' flag w/'qr'
by Laurent_R (Canon) on May 29, 2016 at 21:59 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Adverbs modify how regexes work and give very convenient shortcuts for certain kinds of recurring tasks.OK, I know you are talking of Perl 5, but I think that these explanations clarify the distinction between modifiers which apply to a regex definition and modifiers which apply to the matching process. If you look at the two lists of adverbs, you will see that the equivalent of the g modifier would fit into the matching adverbs category, the equivalent of the :i adverb will be a regex modifier, and :sigspace, the symmetrical counterpart of the x modifier, is a matching adverb. I hope this makes sense. Update; s/^What several months have/What several monks have/. Thanks to AnomalousMonk and LanX for pointing out the typo. | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by perl-diddler (Chaplain) on May 30, 2016 at 01:52 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [reply] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||