The rep() function repeats pattern matches or a rule for a specific number or for one or more matches. The syntax is:

rep(<ruleorpattern>[,<number>])

The rule or pattern to repeat is specified by using <ruleorpattern>. The <number> is optional and indicates the number of times to match the pattern. If the <number> is not included, the pattern is matched one or more times until it fails or until the pattern following it is matched. The pattern must match at least once.

Samples of the rep() function shows some sample uses of the rep() function. Each sample uses the letter P to denote a pattern.

Table 1. Samples of the rep() function

Example

Behavior

Note

rep(P)

P P P P P P ...

Behaves as if a white space operator appears between each occurrence of P.

rep(.P)

P.P.P.P.P.P.P. ...

Behaves as if a dot operator appears between each occurrence of P.

rep(..P)

..P..P..P..P..P..P ...

The double-dot operator ignores all patterns except for P.

The following ASL script matches two numbers, one or more words, and then an end-of-line.

  • The first line of input is matched. There are two numbers followed by two words.

  • The second line of input is matched. There are two numbers and three words. The second rep() function is repeated until the end-of-line is reached.

  • The third line of input fails. The line does not contain two numbers at the front of the line.

    ASL Script (repeat_match.asl):
    START {
     a:rep(integer,2)
     b:rep(word) eol
    }
    do {
     print("Matched numbers ".a);
     print("Matched with ".b);
    }
    DEFAULT {
     ..eol
    }
    do {
     print("Failed match");
    }
    Input (repeat_match.txt):
    3400 4127 cat dog
    4 5 goat lamb cow
    1 chicken horse
    Output:
    $ sm_adapter --file=repeat_match.txt repeat_match.asl
    Matched numbers 3400 4127
    Matched with cat dog
    Matched numbers 4 5
    Matched with goat lamb cow
    Failed match
    $