Using Patterns in SenseTalk
The SenseTalk pattern language lets you search for patterns within a source text with SenseTalk's natural language syntax. For a general description of the pattern language, see SenseTalk Pattern Language Basics. For complete information about how to define patterns, see Elements of the SenseTalk Pattern Language.
Below you will find information for how to use patterns in SenseTalk scripts.
Operators, Commands, and Functions for Pattern Matching
Patterns are supported by all SenseTalk operators that look for the presence or location of some specified text within a larger string, as well as commands and functions that work with text:
set ssn to <3 digits then dash then 2 digits then dash then 4 digits>
put every offset of ssn in textblock into ssnList // Finds the offset (first character position) of all occurrences of ssn pattern match in textblock and places those positions into the ssnList list
You can use any of the following operators, command, and functions with the pattern language:
contains
operatoris in
operatorbegins with
operatorends with
operatoroffset
,range
,every offset
,every range
functionsreplace
commanddelete
commandsplit
command,split by
functionnumber of occurrences of
function
In addition, there are operators and functions specific for use with patterns.
Matches
Operator
Use the matches
operator to test whether a variable or expression is an exact match for a pattern. One of the operands must be a pattern, and the other is treated as a string value. This operator returns true
if the pattern fully matches the entire string. If the pattern matches only part of the string or doesn't match at all, the result of the matches
operator is false
.
Example:
put 83 matches <digit,digit> --> True
put <"x", 3 chars, "y"> matches "xyzzy" --> True
Note that the matches
operator returns true
if the pattern can potentially match the full value, even if the usual match of the pattern might not return the full string (due to lazy quantifiers being used):
put <"$", digits> matches "$895" --> True
put the occurrence of <"$", digits> in "$895" --> "$8"
For information about lazy and greedy matches as well as quantifiers, see Elements of the SenseTalk Pattern Language.