How to: Match a String against a Pattern

If you want to find out if an expression of the String Data Type (Visual Basic) satisfies a pattern, then you can use the Like Operator (Visual Basic).

Like takes two operands. The left operand is a string expression, and the right operand is a string containing the pattern to be used for matching. Like returns a Boolean value indicating whether the string expression satisfies the pattern.

You can match each character in the string expression against a specific character, a wildcard character, a character list, or a character range. The positions of the specifications in the pattern string correspond to the positions of the characters to be matched in the string expression.

To match a character in the string expression against a specific character

  • Put the specific character directly in the pattern string. Certain special characters must be enclosed in brackets ([ ]). For more information, see Like Operator (Visual Basic).

    The following example tests whether myString consists exactly of the single character H.

    Dim sMatch As Boolean = myString Like "H"
    

To match a character in the string expression against a wildcard character

  • Put a question mark (?) in the pattern string. Any valid character in this position makes a successful match.

    The following example tests whether myString consists of the single character W followed by exactly two characters of any values.

    Dim sMatch As Boolean = myString Like "W??"
    

To match a character in the string expression against a list of characters

  • Put brackets ([ ]) in the pattern string, and inside the brackets put the list of characters. Do not separate the characters with commas or any other separator. Any single character in the list makes a successful match.

    The following example tests whether myString consists of any valid character followed by exactly one of the characters A, C, or E.

    Dim sMatch As Boolean = myString Like "?[ACE]"
    

    Note that this match is case-sensitive.

To match a character in the string expression against a range of characters

  • Put brackets ([ ]) in the pattern string, and inside the brackets put the lowest and highest characters in the range, separated by a hyphen (–). Any single character within the range makes a successful match.

    The following example tests whether myString consists of the characters num followed by exactly one of the characters i, j, k, l, m, or n.

    Dim sMatch As Boolean = myString Like "num[i-m]"
    

    Note that this match is case-sensitive.

Matching Empty Strings

Like treats the sequence [] as a zero-length string (""). You can use [] to test whether the entire string expression is empty, but you cannot use it to test if a particular position in the string expression is empty. If an empty position is one of the options you need to test for, you can use Like more than once.

To match a character in the string expression against a list of characters or no character

  1. Call the Like operator twice on the same string expression, and connect the two calls with either the Or Operator (Visual Basic) or the OrElse Operator.

  2. In the pattern string for the first Like clause, include the character list, enclosed in brackets ([ ]).

  3. In the pattern string for the second Like clause, do not put any character at the position in question.

    The following example tests the seven-digit telephone number phoneNum for exactly three numeric digits, followed by a space, a hyphen (–), a period (.), or no character at all, followed by exactly four numeric digits.

    Dim sMatch As Boolean = _
      (phoneNum Like "###[ -.]####") OrElse (phoneNum Like "#######")
    

See Also

Concepts

Operators and Expressions in Visual Basic

Reference

Comparison Operators (Visual Basic)

Like Operator (Visual Basic)

String Data Type (Visual Basic)