Like (operator)

Syntax

expression Like pattern

Description

Compares two strings and returns TRUE if the expression matches the given pattern; returns FALSE otherwise.

Comments

Case sensitivity is controlled by the Option Compare setting.

The pattern expression can contain special characters that allow more flexible matching:

 

Character

Evaluates to

 

?

Matches a single character.

 

*

Matches one or more characters.

 

#

Matches any digit.

 

[range]

Matches if the character in question is within the specified range.

 

[!range]

Matches if the character in question is not within the specified range.

 

A range specifies a grouping of characters. To specify a match of any of a group of characters, use the syntax [ABCDE]. To specify a range of characters, use the syntax [A-Z]. Special characters must appear within brackets, such as []*?#.

If expression or pattern is not a string, then both expression and pattern are converted to String variants and compared, returning a Boolean variant. If either variant is Null, then Null is returned.

The following table shows some examples:

 

expression

TRUE If pattern Is

FALSE If pattern is Is

 

"EBW"

"E*W", "E*"

"E*B"

 

"BasicScript"

"B*[r-t]icScript"

"B[r-t]ic"

 

"Version"

"V[e]?s*n"

"V[r]?s*N"

 

"2.0"

"#.#", "#?#"

"###", "#?[!0-9]"

 

"[ABC]"

"[[]*]"

"[ABC]", "[*]"

Example

This example demonstrates various uses of the Like function.

Sub Main()
  a$ = "This is a string variable of 123456 characters"
  b$ = "123.45"
  If a$ Like "[A-Z][g-i]*" Then MsgBox "The first comparison is True."
  If b$ Like "##3.##" Then MsgBox "The second comparison is True."
  If a$ Like "*variable*" Then MsgBox "The third comparison is True."
End Sub

See Also

Operator Precedence (topic); Is (operator); Option Compare (statement).

More information

L