in reply to 'g' flag w/'qr'
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).
| Option | Pertains to | (?:) | qr// | m | s | tr |
|---|---|---|---|---|---|---|
| m | meaning of regular expression pattern | Yes | Yes | Yes | Yes | |
| s | meaning of regular expression pattern | Yes | Yes | Yes | Yes | |
| i | meaning of regular expression pattern | Yes | Yes | Yes | Yes | |
| x | meaning of regular expression pattern | Yes | Yes | Yes | Yes | |
| p | meaning of regular expression pattern | Yes | Yes | Yes | Yes | |
| a/d/l/u | meaning of regular expression pattern | Yes | Yes | Yes | Yes | |
| n | meaning of regular expression pattern | Yes | Yes | Yes | Yes | |
| o | compiling of regular expression patterns | Yes | Yes | Yes | ||
| c | matching of regular expression patterns | Yes | Yes | |||
| g | matching of regular expression patterns | Yes | Yes | |||
| e | replacement expression | Yes | ||||
| ee | replacement expression | Yes | ||||
| r | input modification | Yes | Yes | |||
| c | transliteration | Yes | ||||
| d | transliteration | Yes | ||||
| s | transliteration | Yes | ||||
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: 'g' flag w/'qr'
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] |