MATCHES operator tests whether a character value matches a quoted string:
mask |
a character expression returning a string that can include literal characters, wild cards, etc. |
char |
a single character enclosed between a pair of single (') or double (") quotation marks (specifies an escape symbol) |
If an operand has a null value, the entire string comparison returns NULL.
mask can include the asterisk (*) and question mark (?) symbols. The asterisk (*) can represent any number of characters, and ? replaces only one character. If you want to use * or ? as literal characters, you must put a backslash (\) in front of them.
This piece of code tests whether the string contains an question mark. The backslash (\) is necessary because (?) is a wild card with MATCHES:
IF string MATCHES "* \? *" THEN ...
If you put square brackets [] around a group of characters, all these characters will be matched.
If you put a hyphen between characters that are inclosed in square brackets – e.g., [f-k], – all characters in this range will be matched (is case-sensitive).
If you put a caret (^) at the beginning of a character sequence inclosed in square brackets – e.g., [^xyz], – any characters will be matched excluding those in the brackets.
For example,
LET v_string = "abcde12345ABCDE"
IF v_string MATCHES "abc*" THEN DISPLAY "1. TRUE" ELSE DISPLAY "1. FALSE" END IF --> will be displayed TRUE
IF v_string NOT MATCHES "*45AB*" THEN DISPLAY "2. TRUE" ELSE DISPLAY "2. FALSE" END IF --> will be displayed FALSE
IF v_string MATCHES "ab?de12??5AB?DE" THEN DISPLAY "3. TRUE" ELSE DISPLAY "3. FALSE" END IF --> will be displayed TRUE
IF v_string MATCHES "ab[cCx]de12[639]45ABCDE" THEN DISPLAY "4. TRUE" ELSE DISPLAY "4. FALSE" END IF --> will be displayed TRUE
IF v_string MATCHES "ab[a-z]de12[1-9]45AB[A-Z]DE" THEN DISPLAY "5. TRUE" ELSE DISPLAY "5. FALSE" END IF --> will be displayed TRUE
IF v_string MATCHES "ab[^abde]de12[^1245]45AB[^ABDE]DE" THEN DISPLAY "6. TRUE" ELSE DISPLAY "6. FALSE" END IF --> will be displayed TRUE
This WHERE clause tests the contents of character field field1 for the string board. So, the wild card (*) specifies that the comparison is true if board appears alone or in longer strings such as boarding or game board:
COLOR = RED WHERE field1 MATCHES "*board*"
You can replace the backslash (\) as the literal symbol. If you include an ESCAPE char clause in an expression with LIKE or MATCHES, 4gl interprets the character that follows char as a literal even if that character corresponds to a special symbol of LIKE or MATCHES. A double quotation mark (") cannot be used as a CHAR variable.
If you specify ESCAPE "z", the character strings z_ and z? in a string will stand for the literal characters _ and ? rather than be wild cards. Similarly, character strings z% and z* stand for the characters % and *. Finally, the character string zz stand for the single character z.
For example,
LET v_string = "abc * ? [ ] % _ \\ def"
IF v_string MATCHES "* z* *" ESCAPE "z" THEN DISPLAY "1. TRUE" ELSE DISPLAY "1. FALSE" END IF --> will be displayed TRUE
IF v_string MATCHES "* z? *" ESCAPE "z" THEN DISPLAY "2. TRUE" ELSE DISPLAY "2. FALSE" END IF --> will be displayed TRUE
IF v_string NOT MATCHES "* z[ *" ESCAPE "z" THEN DISPLAY "3. TRUE" ELSE DISPLAY "3. FALSE" END IF --> will be displayed FALSE
IF v_string MATCHES "* z] *" ESCAPE "z" THEN DISPLAY "4. TRUE" ELSE DISPLAY "4. FALSE" END IF --> will be displayed TRUE
IF v_string MATCHES "* z\\ *" ESCAPE "z" THEN DISPLAY "5. TRUE" ELSE DISPLAY "5. FALSE" END IF --> will be displayed TRUE