Pattern Match Operator

The pattern match operator (?) causes M to return a TRUE if the expression ahead of the operator matches the characteristics described by the pattern following the operator. The pattern is not an expression.

Patterns are made up of two elements:

  1. A repetition count

  2. A pattern code, a string literal or an alternation list

The element following the pattern match operator may consist of an indirection operator, followed by an element that evaluates to a legitimate pattern.

The repetition count consists of either a single integer literal or a period (.) delimiter with optional leading and trailing integer literals. A single integer literal specifies an exact repetition count. The period syntax specifies a range of repetitions where the leading number is a minimum and the trailing number is a maximum. When the repetition count is missing the leading number, M assumes there is no minimum, (i.e., a minimum of zero). When the repetition count is missing the trailing number, M does not place a maximum on the number of repetitions.

The pattern codes are:

A alphabetic characters upper or lower case

C control characters ASCII 0-31 and 127

E any character; used to pass all characters in portions of the string where the pattern is not restricted

L lower-case alphabetic characters, ASCII 97-122

N digits 0-9, ASCII 48-57

P punctuation, ASCII 32-47, 58-64, 91-96, 123-126

U upper-case alphabetic characters, ASCII 65-90

Pattern codes may be upper or lower case and may be replaced with a string literal. GT.M allows the M pattern match definition of patcodes A, C, N, U, L, and P to be extended or changed, (A can only be modified implicitly by modifying L or U) and new patcodes added. For detailed information on enabling this functionality, refer to the "Internationalization" chapter in this manual.

[Note]

The GT.M compiler accepts pattern codes other than those explicitly defined above. If, at run-time, the pattern codes come into use and no pattern definitions are available, GT.M issues a run-time error (PATNOTFOUND). GT.M does not currently implement a mechanism for Y and Z patterns and continues to treat those as compile-time syntax errors.

Example:

        GTM>WRITE "ABC"?3U
        1
        GTM>WRITE "123-45-6789"?3N1"-"2N1"-"4N
        1
        

The first WRITE has a simple one-element pattern while the second has multiple elements including both codes and string literals. All the repetition counts are fixed.

Example:

          I x?.E1C.E W !,"Must not contain a control character" Q
        

This example uses a pattern match to test for control characters.

Example:

       I acn?1U.20A1","1U.10A D
       .S acn=$G((^ACX($P(acn,","),$P(acn,",",2)))
        

This example uses a pattern match with implicit minimums to determine that an "account number" is actually a name, and to trigger a look-up of the corresponding account number in the ^ACX cross index.

The pattern match operator accepts the alteration syntax. Alteration consists of a repeat count followed by a comma-delimited list of patatoms enclosed in parentheses "()". The semantic is that the pattern matches if any of the listed patterns matches the operand string. For example, ?1(2N1"-"7N,3N1"-"2N1"-"4N).1U might be a way to match either a social security number or a taxpayer ID. Since alternation is defined as one of the ways of constructing a patatom, alternation can nest (be used recursively).

[Note]

Complex pattern matches may not be efficient to evaluate, so every effort should be made to simplify any commonly used pattern and to determine if more efficient alternative logic would be more appropriate.