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.”
USER>set ssn = "012-34-5678"
USER>if ssn?3N1"-"2N1"-"4N {write "valid"}
valid
USER>set ht1 = "6 feet 2 inches", ht2 = "5' 10"""
USER>if ht1?1N1(1" feet",1"'")1" "1.2N1(1" inches",1"""") {write "valid"}
valid
USER>if ht2?1N1(1" feet",1"'")1" "1.2N1(1" inches",1"""") {write "valid"}
valid
USER>set last1 = "O'Reilly", last2 = "McMahon 3rd", last3 = "/////32351abcde"
USER>if last1?.ANP {write "valid"} ; good name
valid
USER>if last2?.ANP {write "valid"} ; good name
valid
USER>if last3?.ANP {write "valid"} ; bad name (false positive)
valid
USER>set c = "InterSystems" if c?1"Inter".E {write "valid"}
valid
USER>
You could use the following complex pattern to validate last names such as the ones in the previous example (O'Reilly, McMahon 3rd), while rejecting the other strings that the simple .ANP pattern accepts.
USER>set last1 = "O'Reilly", last2 = "McMahon 3rd", last3 = "/////32351abcde"
USER>if last1?.1(1"O'",1"Mc")1U.L.1(1" "1(1"Sr",1"Jr",1"3rd",1"4th")) {write "valid"}
valid
USER>if last2?.1(1"O'",1"Mc")1U.L.1(1" "1(1"Sr",1"Jr",1"3rd",1"4th")) {write "valid"}
valid
USER>if last3?.1(1"O'",1"Mc")1U.L.1(1" "1(1"Sr",1"Jr",1"3rd",1"4th")) {write "valid"}
USER>