like
like, operator
The binary candidate like pattern
pure operator returns true if the text candidate
matches the pattern described by the text pattern
. Patterns interpret *
to mean zero, one or more characters, and ?
to mean exactly one character.
The unary like pattern
is used as a pattern in a match
statement, where it is translated to the binary form with the match
expression as left operand.
Examples
As an expression
table Test = with
[| as Text, as Pattern |]
[| "ABCDE", "AB?DE" |]
[| "ABCDE", "A?E" |]
[| "ABCDE", "A*E" |]
[| "ABCDE", "ABCD*E" |]
[| "ABCDECDE", "A*C*E" |]
[| "ABCDE", "*CDE" |]
[| "ABCDE", "ABC*" |]
[| "ABCDE", "*C*" |]
show table "Like" with
Test.Text
Test.Pattern
Test.Text like Test.Pattern
Produces the following output:
Text | Pattern | Test.Text like Test.Pattern | |
---|---|---|---|
ABCDE | AB?DE | true | ’?’ matches C |
ABCDE | A?E | false | ’?’ cannot match BCD |
ABCDE | A*E | true | ’*’ matches BCD |
ABCDE | ABCD*E | true | ’*’ matches zero characters |
ABCDECDE | A*C*E | true | ’*’ & ‘*’ match BCDE & D, or B & DECD |
ABCDE | *CDE | true | ’*’ matches AB |
ABCDE | ABC* | true | ’*’ matches DE |
ABCDE | *C* | true | ’*’ & ‘*’ match AB & DE |
As a match pattern
table Test with
[| as Text |]
[| "A" |]
[| "AB" |]
[| "CDE" |]
[| "EDCB" |]
[| "XEY" |]
[| "E" |]
Test.Matched = match Test.Text with
like "A*" -> "A*"
like "*B" -> "*B"
like "*D*" -> "*D*"
like "?E?" -> "?E?"
.. -> "Other"
show table "Like" with
Test.Text
Test.Matched
Produces the following output:
Text | Matched | |
---|---|---|
A | A* | ’*’ matches zero characters |
AB | A* | ’*’ matches B, does not reach second line of match |
CDE | *D* | ’*’ & ‘*’ match C and E |
EDCB | *B | ’*’ matches EDC, does not reach third line of match |
XEY | ?E? | ’?’ and ‘?’ match X and Y |
E | Other |
Remarks
Forbidden characters
This operator is similar to the SQL LIKE operator, but does not use the the same special characters: SQL uses %
instead of *
to match an arbitrary number of characters, and _
instead of ?
to match exactly one character. To avoid confusion, the characters %
and _
are not allowed inside a like
pattern.
To allow for future expansion, the characters \
, [
and (
are also not allowed inside a like
pattern.
Escaping characters
To use a forbidden character inside a like
pattern, or to represent the *
and ?
characters without their special meaning, they must be escaped by surrounding them with []
. Only one character is allowed between the square brackets, and that character is expected to be matched verbatim.
show summary "Escaping" with
"ABC" like "A*" // true
"ABC" like "A[*]" // false
"A*" like "A[*]" // true
"$" like "[$]" // error: $ does not need to be escaped.
"[$]" like "[[]$]" // true
"_" like "[_]" // true
"_" like "_" // error: _ needs to be escaped.
"__" like "[__]" // error: only one character allowed in [ ]
"__" like "[_][_]" // true
Use function escapelike
to escape the contents of a text variable so that it can be safely included in a like
pattern.
Optimization
Since there is a cost to parsing arbitrary patterns at runtime, Envision automatically detects if a compile-time constant pattern can be converted to more optimized function call. For example:
A like "*BCD*"
is a synonym forcontains(A, "BCD")
A like "BCD*"
is a synonym forstartsWith(A, "BCD")
A like "*BCD"
is a synonym forendsWith(A, "BCD")
A like "B*C"
is a synonym forstartsWith(A, "B") and endsWith(A, "C")
A like "??"
is a synonym forstrlen(A) == 2
…and so on. Therefore, it is advisable to use like
instead of text functions whenever that ends in more readable code.
Errors
When a forbidden character (_
, %
, (
, \
, [
) is used without being escaped:
`like`: character “%” must be escaped as “[%]”
When a character escape block (starting with [
and ending with ]
) contains more than one, or less than one, characters:
`like`: use “[” and “]” to escape exactly one character at a time.
When a character escape block contains a character that does not need to be escaped:
`like`: in “[A]”, character “A” does not need to be escaped. Did you mean “A” or “[[]A]” ?