Character(s) in pattern |
Matches in expression |
(?) DAO (_) ADO |
Any single character |
(*) DAO (%) ADO |
Zero or more characters |
# |
Any single digit (0-9) |
[charlist] |
Any single character in charlist |
[!charlist] |
Any single character not in charlist |
A group of one or more characters (charlist) enclosed in brackets ([ ]) can
be used to match any single character in expression and can include almost any
characters in the ANSI character set, including digits. In fact, the special
characters left bracket ([ ), question mark (?), number sign (#), and asterisk
(*) can be used to match themselves directly only by enclosing them in brackets.
The right bracket ( ]) cannot be used within a group to match itself, but it can
be used outside a group as an individual character.
In addition to a simple list of characters enclosed in brackets, charlist can
specify a range of characters by using a hyphen (-) to separate the upper and
lower bounds of the range. For example, [A-Z] in pattern results in a match if
the corresponding character position in expression contains any of the uppercase
letters in the range A through Z. Multiple ranges are included within the
brackets without any delimiting. For example, [a-zA-Z0-9] matches any
alphanumeric character.
Other important rules for pattern matching include the following:
An exclamation mark (!) at the beginning of charlist means that a match is
made if any character except the ones in charlist are found in expression. When
used outside brackets, the exclamation point matches itself.
The hyphen (-) can appear either at the beginning (after an exclamation mark
if one is used) or at the end of charlist to match itself. In any other
location, the hyphen is used to identify a range of ANSI characters.
When a range of characters is specified, they must appear in ascending sort
order (from lowest to highest). [A-Z] is a valid pattern, but [Z-A] is not.
The character sequence [ ] is ignored; it is considered to be a zero-length
string.
The following examples show how you can use Like to test expressions for different patterns.
Kind of matching | With this pattern |
This expression returns True |
This expression returns False |
Multiple characters |
a*a |
"aa", "aBa", "aBBBa" |
"aBC" |
Special character |
a[*]a |
"a*a" |
"aaa" |
Multiple characters |
ab* |
"abcdefg", "abc" |
"cab", "aab" |
Single character |
a?a |
"aaa", "a3a", "aBa" |
"aBBBa" |
Single digit |
a#a |
"a0a", "a1a", "a2a" |
"aaa", "a10a" |
Range of characters |
[a-z] |
"f", "p", "j" |
"2", "&" |
Outside a range |
[!a-z] |
"9", "&", "%" |
"b", "a" |
Not a digit |
[!0-9] |
"A", "a", "&", "~" |
"0", "1", "9" |
Combined |
a[!b-m]# |
"An9", "az0", "a99" |
"abc", "aj0" |
Single special to double |
[?] |
"ae", "AE", "?" |
"Ä", "A" |