Match PowerScript function
Description
Determines whether a string’s value contains a particular
pattern of characters.
Syntax
1 |
<span>Match</span> ( <span>string</span>, <span>textpattern</span> ) |
Argument |
Description |
---|---|
string |
The string in which you want to look |
textpattern |
A string whose value is the text pattern |
Return Values
Boolean. Returns true if string matches textpattern and false if
it does not. Match also returns false if
either argument has not been assigned a value or the pattern is
invalid. If any argument’s value is null, Match returns null.
Usage
Match enables you to evaluate whether a
string contains a general pattern of characters. To find out whether
a string contains a specific substring, use the Pos function.
Textpattern is similar to a regular expression.
It consists of metacharacters, which have special meaning, and ordinary
characters, which match themselves. You can specify that the string
begin or end with one or more characters from a set, or that it
contain any characters except those in a set.
A text pattern consists of metacharacters, which have special
meaning in the match string, and nonmetacharacters, which match
the characters themselves.The following tables explain the meaning
and use of these metacharacters.
Metacharacter |
Meaning |
Example |
---|---|---|
Caret (^) |
Matches the beginning of a string |
^C matches C at the beginning of a string. |
Dollar sign ($) |
Matches the end of a string |
s$ matches s at the end of a string. |
Period (.) |
Matches any character |
. . . matches three consecutive characters. |
Backslash () |
Removes the following metacharacter’s |
$ matches $. |
Character class (a group of characters enclosed |
Matches any of the enclosed characters |
[AEIOU] matches A, You can use hyphens to abbreviate ranges of characters in |
Complemented character class (first character |
Matches any character not in the group following |
[^0-9] matches any |
The metacharacters asterisk (*), plus (+),
and question mark (?) are unary operators that are used to specify
repetitions in a regular expression:
Metacharacter |
Meaning |
Example |
---|---|---|
* (asterisk) |
Indicates zero or more occurrences |
A* matches zero or more As (no As, |
+ (plus) |
Indicates one or more occurrences |
A+ matches one A or more than one |
? (question mark) |
Indicates zero or one occurrence |
A? matches an empty string (“”) or A |
Sample patterns
The following table shows various text patterns and sample text
that matches each pattern:
This pattern |
Matches |
---|---|
AB |
Any string that contains AB; for example, |
B* |
Any string that contains 0 or more Bs; |
AB*C |
Any string containing the pattern AC |
AB+C |
Any string containing the pattern ABC |
ABB*C |
Any string containing the pattern ABC |
^AB |
Any string starting with AB |
AB?C |
Any string containing the pattern AC |
^[ABC] |
Any string starting with A, B, or C |
[^ABC] |
A string containing any characters other |
^[^abc] |
A string that begins with any character |
^[^a-z]$ |
Any single-character string that is not |
[A-Z]+ |
Any string with one or more uppercase |
^[0-9]+$ |
Any string consisting only of digits |
^[0-9][0-9][0-9]$ |
Any string consisting of exactly three |
^([0-9][0-9][0-9])$ |
Any consisting of exactly three digits |
Examples
This statement returns true if
the text in sle_ID begins with one
or more uppercase or lowercase letters (^ at the beginning of the
pattern means that the beginning of the string must match the characters
that follow):
1 |
<span>Match</span>(sle_ID.Text, "^[A-Za-z]") |
This statement returns false if
the text in sle_ID contains any digits
(^ inside a bracket is a complement operator):
1 |
<span>Match</span>(sle_ID.Text, "[^0-9]") |
This statement returns true if
the text in sle_ID contains one uppercase
letter:
1 |
<span>Match</span>(sle_ID.Text, "[A-Z]") |
This statement returns true if
the text in sle_ID contains one or
more uppercase letters (+ indicates one or more occurrences
of the pattern):
1 |
<span>Match</span>(sle_ID.Text, "[A-Z]+") |
This statement returns false if
the text in sle_ID contains anything
other than two digits followed by a letter (^ and $ indicate
the beginning and end of the string):
1 |
<span>Match</span>(sle_ID.Text, "^[0-9][0-9][A-Za-z]$") |
See Also
-
Match method for DataWindows
in the DataWindow Reference or the online Help