7.3 Modifiers

     

Modifiers alter the meaning of the pattern syntax. The standard position for modifiers is at the beginning of the rule, right after the m , s , or rx , or after the name in a named rule. Modifiers cannot attach to the outside of a bare / . . . / . For example:

 m:i/marvin/ # case insensitive rule names :i { marvin  ford  arthur } 

The single-character modifiers can be grouped, but the others must be separated by a colon :

 m:wig/ zaphod /                        # OK m:words:ignorecase:globally / zaphod / # OK m:wordsignorecaseglobally / zaphod /   # Not OK 

Most of the modifiers can also go inside the rule, attached to the rule delimiters or to grouping delimiters. Internal modifiers are lexically scoped to their enclosing delimiters, so you get a temporary alteration of the pattern:

 m/:w I saw [:i zaphod] / # only 'zaphod' is case insensitive 

The repetition modifiers (: N x , : N th , :once , :globally , and :exhaustive ) and the continue modifier ( :cont ) can't be lexically scoped, because they alter the return value of the entire rule.

The : N x modifier matches the rule a counted number of times. If the modifier expects more matches than the string has, the match fails. It has an alternate form :x( N ), which can take a variable in place of the number.

The :once modifier on a rule only allows it to match once. The rule will not match again until you call the .reset method on the rule object.

The :globally modifier matches as many times as possible. The :exhaustive modifier also matches as many times as possible, but in as many different ways as possible.

The : N th modifier preserves one result from a particular counted match. If the rule matches fewer times than the modifier expects, the match fails. It has several alternate forms. One form ” :th( N ) ”can take a variable in place of the number. The other forms ”: N st , : N nd , and : N rd ”are for cases where it's more natural to write :1st , :2nd , :3rd than it is to write :1th , :2th , :3th . Either way is valid, so pick the one that's most comfortable for you.

By default, rules ignore literal whitespace within the pattern. The :w modifier makes rules sensitive to literal whitespace, but in an intelligent way. Any cluster of literal whitespace acts like an explicit \s+ when it separates two identifiers and \s* everywhere else.

There are no modifiers to alter whether the matched string is treated as a single line or multiple lines. That's why the "beginning of string" and "end of string" metasymbols have "beginning of line" and "end of line" counterparts.

Table 7-6 shows the current list of modifiers.

Table 7-6. Modifiers

Short

Long

Meaning

:i

:ignorecase

Case-insensitive match.

:I

 

Case-sensitive match (on by default).

:c

:cont

Continue where the previous match on the string left off.

:w

:words

Literal whitespace in the pattern matches as \s+ or \s* .

:W

 

Turn off intelligent whitespace matching (return to default).

 

: N x / :x( N )

Match the pattern N times.

 

: N th / :nth( N )

Match the Nth occurrence of a pattern.

 

:once

Match the pattern only once.

:g

:globally

Match the pattern as many times as possible, but only possibilities that don't overlap

:e

:exhaustive

Match every possible occurrence of a pattern, even overlapping possibilities.

 

:u0

. is a byte.

 

:u1

. is a Unicode codepoint.

 

:u2

. is a Unicode grapheme.

 

:u3

. is language dependent.

 

:p5

The pattern uses Perl 5 regex syntax.




Perl 6 and Parrot Essentials
Perl 6 and Parrot Essentials, Second Edition
ISBN: 059600737X
EAN: 2147483647
Year: 2003
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net