Like Operator

See Also41OX951              Example3DPLLXQ>Low

Used to compare two string expressions1330R89.

Syntax

result = expression Like pattern

Remarks

If expression matches pattern, result is True; if there is no match, result is False; and if either expression or pattern is a Null1DDW7C0, result is also a Null.  The case sensitivity and character sort order1O1WAUW of the Like operator depend on the setting of the Option Compare statement.  Unless otherwise specified, the default string-comparison0QKI7U method for each module2JU32VW is Option Compare Binary; that is, string comparisons are case-sensitive.

Built-in pattern matching provides a versatile tool for string comparisons.  The pattern-matching features allow you to use wildcard characters, such as those recognized by the operating system, to match strings.  The wildcard characters and what they match are shown in the following table:

 

Character(s)   Matches in
in pattern        expression

 

?                     Any single character

*                      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 set108ABF, 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 point (!) 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 ANSI5221FB 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.

 

In some languages, there are special characters in the alphabet that actually represent two separate characters.  For example, several languages use the character " " to represent the characters "a" and "e" when they appear together.  The Like operator recognizes that the single special character and the two individual characters are equivalent.

When a language that uses one of these special characters is specified in the WIN.INI file (sLanguage), an occurrence of the special single character in either pattern or expression matches the equivalent 2-character sequence in the other string.  Similarly, a special single character in pattern enclosed in brackets (by itself, in a list, or in a range), matches the equivalent 2-character sequence in expression.


See Also

Arithmetic OperatorsSH0K1B

Comparison OperatorsNRJZ1K

Concatenation Operators2MYTW1K

Instr FunctionPSLW56

Logical Operators1CQVUTN

Operator Precedence23082A9

Option Compare Statement1EF8XDE

StrComp Function4EP49UH


Like Operator Examples

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"