5.6.9.6 Like Operator

The Like operator is a simple data operator that performs a string matching test of the source string in the left operand against the pattern string in the right operand.

 like-operator-expression = expression "like" like-pattern-expression 
 like-pattern-expression = expression 

Static semantics:

  • The Like operator is statically resolved as a simple data operator.

  • A Like operator expression is invalid if the declared type of any operand is an array or a UDT.

  • A Like operator has the following declared type, based on the declared type of its operands:

    Left Operand Declared Type

    Right Operand Declared Type

    Operator Declared Type

    Any type except an array, UDT or Variant

    Any type except an array, UDT or Variant

    Boolean

    Any type except an array or UDT

    Variant

    Variant

    Variant

    Any type except an array or UDT

    Variant

 

Runtime semantics:

  • The Like operator is first evaluated as a simple data operator.

  • If either <expression> or <like-pattern-expression> is Null, the result is Null.

  • Otherwise, <expression> and <like-pattern-expression> are both Let-coerced to String. The grammar for the String value of <like-pattern-expression> is interpreted as <like-pattern-string>, according to the following grammar:

     like-pattern-string = *like-pattern-element 
     like-pattern-element = like-pattern-char / "?" / "#" / "*" / like-pattern-charlist 
     like-pattern-char = <Any character except "?", "#", "*" and "[" > 
     like-pattern-charlist = "[" ["!"] ["-"] *like-pattern-charlist-element ["-"] "]" 
     like-pattern-charlist-element = like-pattern-charlist-char / like-pattern-charlist-range 
     like-pattern-charlist-range = like-pattern-charlist-char "-" like-pattern-charlist-char 
     like-pattern-charlist-char = <Any character except "-" and "]">   
    
  • The pattern in <like-pattern-expression> is matched one <like-pattern-element> at a time to the characters in <expression> until either:

    • All characters of <expression> and <like-pattern-expression> have been matched. In this case, the result is True.

    • Either <expression> or <like-pattern-expression> is fully matched, while the other string still has unmatched characters. In this case, the result is False.

    • A <like-pattern-element> does not match the next characters in <expression>. In this case, the result is False.

    • The next characters in <like-pattern-expression> do not form a valid, complete <like-pattern-element> according to the grammar. In this case, runtime error 93 (Invalid pattern string) is raised. Note that this runtime error is only raised if no other result has been produced before pattern matching proceeds far enough to encounter this error in the pattern.

  • String matching uses the Option Compare comparison mode (section 5.2.1.1) setting of the enclosing module, as well as any implementation-defined regional settings related to text collation. When the comparison mode is text-compare-mode (section 5.2.1.1), some number of actual characters in <expression> can match a different number of characters in the pattern, according to the host-defined regional text collation settings. This means that the single pattern character "æ" can match the expression characters "ae". A pattern character can also match just part of an expression character, such as the two pattern characters "ae" each matching part of the single expression character "æ".

  • Each <like-pattern-element> in the pattern has the following meaning:

    Pattern element

    Meaning

    <like-pattern-char>

    Matches the specified character.

    ?

    Matches any single actual character in the expression, or the rest of a partially matched actual character.

     

    When the comparison mode is text-compare-mode, the ? pattern element matches all the way to the end of one actual character in <expression>, which can be just the last part of a partially matched expression character. This means that the expression "æ" can be matched by the pattern "a?", but might not be matched by the pattern "?e".

    #

    Matches a single character representing a digit. 

    *

    Matches zero or more characters.

     

    When a * pattern element is encountered, the rest of the pattern is immediately checked to ensure it can form a sequence of valid, complete <like-pattern-element> instances according to the grammar. If this is not possible, runtime error 93 (Invalid pattern string) is raised.

     

    When the comparison mode is text-compare-mode, the * pattern element can match part of a character. This means that the expression "æ" can be matched by the pattern "a*" or the pattern "*e".

    <like-pattern-charlist>

    Matches one of the characters in the specified character list.

     

    A <like-pattern-charlist> contains a sequence of <like-pattern-charlist-element> instances, representing the set of possible characters that can be matched. Each <like-pattern-charlist-element> can be one of the following:

    • <like-pattern-charlist-char>: This adds the specified character to the character list.

    • <like-pattern-charlist-range>: This adds a range of characters to the character list, including all characters considered greater than or equal to the first <like-pattern-charlist-char> and considered less than or equal to the second <like-pattern-charlist-char>. If the end character of this range is considered less than the start character, runtime error 93 (Invalid pattern string) is raised. Semantics are undefined if a compound character such as "æ" that can match multiple expression characters is used within a <like-pattern-charlist-range> when the comparison mode is text-compare-mode

    If the optional "-" is specified at the beginning or end of <like-pattern-charlist>, the character "-" is included in the character list.

     

    If the optional "!" is specified at the beginning of <like-pattern-charlist>, this pattern element will instead match characters not in the specified character list.

     

    When the comparison mode is text-compare-mode, the first specified element of the character list that can match part of the actual expression character is chosen as the match. This means that the expression "æ" can be matched by the pattern "a[ef]" or "[æa]", but might not be matched by the pattern "[aæ]".