match Function
Searches for a string or expression within another string.
int match(str pattern, str text)
The system does not differentiate between lower and upper case.
The following special characters can be used to create the pattern in the fixed expression:
|
Character |
Description |
|---|---|
|
\ |
A backslash causes a specific character to be matched. Remember to escape backslashes. For example: match("ab$cd","ab$cd"); //returns 0 match("ab\$cd","ab$cd"); //returns 0 - the backslash is not escaped match("ab\\$cd","ab$cd"); //returns 1 - the backslash and dollar sign are escaped |
|
< or ^ |
A 'less than'(<) sign or a circumflex (^) at the start of an expression is used to match the start of a line. For example: match("<abc","abcdef"); //returns 1 match("<abc","defabc"); //returns 0 match("^abc","abcdef"); //returns 1 match("^abc","defabc"); //returns 0 |
|
> |
A 'greater than'(>) sign at the end of the expression is used to match the end of a line. For example: match("abc>","abcdef"); //returns 0 match("abc>","defabc"); //returns 1 |
|
? or . |
A question mark (?) or a full stop (.) will match any character. For example: match("abc.def","abc#def"); //returns 1 match("colou?r","colouXr"); //returns 1 |
|
:x |
A colon specifies a group of characters to be matched, indicated by the character which follows immediately after. |
|
:a |
Sets the match to letters. For example: match("ab:acd","ab#cd"); //returns 0 match("ab:acd","abxyzcd"); //returns 0 match("ab:acd","abxcd"); //returns 1 |
|
:d |
Sets the match to numeric characters. For example: match("ab:dcd","ab3cd"); //returns 1 match("ab:dcd","ab123cd"); //returns 0 match("ab:dcd","abcd"); //returns 0 |
|
:n |
Sets the match to alphanumeric characters. For example: match("ab:ncd","ab%cd"); //returns 0 match("ab:ncd","ab9cd"); //returns 1 match("ab:ncd","abXcd"); //returns 1 |
|
:SPACE |
Where SPACE is the character ' '. Sets the match to blanks, tabulations, and control characters such as Enter (new line). For example: match("ab: cd","ab cd"); //returns 1 match("ab: cd","ab\ncd"); //returns 1 match("ab: cd","ab\tcd"); //returns 1 match("ab: cd","ab cd"); //returns 0 - only the first space is matched |
|
* |
An expression followed by an asterisk requires a match for none, one, or more occurrences of the preceding expression. For example: match("abc*d","abd"); //returns 1 match("abc*d","abcd"); //returns 1 match("abc*d","abcccd"); //returns 1 match("abc*d","abxd"); //returns 0 |
|
+ |
An expression followed by a plus (+) sign requires a match for one or more occurrences of the preceding expression. For example: match("abc+d","abd"); //returns 0 match("abc+d","abcd"); //returns 1 match("abc+d","abcccd"); //returns 1 match("abc+d","abxd"); //returns 0 |
|
- |
An expression followed by a minus (-) sign requires a match for one or no occurrences of the preceding expression. Basically, the preceding expression is optional. For example: match("colou-r","color"); //returns 1 match("colou-r","colour"); //returns 1 |
|
[] |
Matches a single character with any character contained within the brackets.A range of characters can be specified by two characters separated by '-' (minus). For example, "[a-z]" matches all letters between a and z, [0-9] matches a digit, [0-9a-f] matches a hexadecimal digit. match("[abc]","apple"); //returns 1 - matches the 'a' in apple match("[abc]","kiwi"); //returns 0 - kiwi does not contain an a, b, or c match("gr[ae]y","grey"); //returns 1 - also matches "gray" match("gr[ae]y","graey"); //returns 0 - only one character between "gr" and "y" is matched. |
|
[^] |
If the first character in a text within square brackets is a circumflex (^), the expression matches all characters except those contained within the brackets. match("[^bc]at","bat"); //returns 0 match("[^bc]at","hat"); //returns 1 match("[^abc]","bat"); //returns 1 - anything but a, b, or c is matched. The t is matched |