Skip to main content

Pattern Matching Examples

Here are some examples of good (and bad) patterns to illustrate how the pattern match operator works:

  • A Social Security Number is 3 numbers, 1 dash, 2 numbers, 1 dash, and 4 numbers. A typical beginner error is to specify the pattern like this: 3N"-"2N"-"4N, or this: 3N1P2N1P4N. The former pattern is missing the count for the literal dash character; the latter will allow any punctuation to separate the numbers, not just the dash.

  • Either words or symbols can express height in feet and inches. Note that to enclose quotation marks in quotes (in the ht2 variable and within the pattern), you must use double quotation marks.

  • Last names usually contain only letters, but may contain punctuation and numbers. The .ANP pattern will accept valid last names, but also accepts strings that could never be last names (false positives).

  • If you need to verify only the beginning of a string, your pattern should end with .E, meaning “any number of any character.”

SAMPLES>set ssn = "012-34-5678"

SAMPLES>if ssn?3N1"-"2N1"-"4N write "valid"
valid
SAMPLES>set ht1 = "6 feet 2 inches", ht2 = "5' 10"""

SAMPLES>if ht1?1N1(1" feet",1"'")1" "1.2N1(1" inches",1"""") {write "valid"}
valid
SAMPLES>if ht2?1N1(1" feet",1"'")1" "1.2N1(1" inches",1"""") {write "valid"}
valid
SAMPLES>set last1 = "O'Reilly-McMahon 3rd", last2 = "/////32351abcde"

SAMPLES>if last1?.ANP {write "valid"} ; good name
valid
SAMPLES>if last2?.ANP {write "valid"} ; bad name
valid
SAMPLES>set c="InterSystems" if c?1"Inter".E {write "valid"}
valid
SAMPLES>
FeedbackOpens in a new tab