Wild-Card Matching

AutoCAD AutoLISP & Visual LISP

 
Wild-Card Matching
 
 
 

The wcmatch function enables applications to compare a string to a wild-card pattern. You can use this facility when you build a selection set (in conjunction with ssget) and when you retrieve extended entity data by application name (in conjunction with entget).

The wcmatch function compares a single string to a pattern. The function returns T if the string matches the pattern, and nil if it does not. The wild-card patterns are similar to the regular expressions used by many system and application programs. In the pattern, alphabetic characters and numerals are treated literally; brackets can be used to specify optional characters or a range of letters or digits; a question mark (?) matches a single character; an asterisk (*) matches a sequence of characters; and, certain other special characters have special meanings within the pattern. When you use the * character at the beginning and end of the search pattern, you can locate the desired portion anywhere in the string.

In the following examples, a string variable called matchme has been declared and initialized:

_$ (setq matchme "this
is a string - test1 test2 the end")
"this is a string - test1 test2 the end" 

The following code checks whether or not matchme begins with the four characters "this":

_$ (wcmatch matchme
"this*")
T 

The following code illustrates the use of brackets in the pattern. In this case, wcmatch returns T if matchme contains "test4", "test5", "test6" (4-6), or "test9" (note the use of the * character):

_$ (wcmatch matchme
"*test[4-69]*")
nil 

In this case, wcmatch returns nil because matchme does not contain any of the strings indicated by the pattern.

However,

_$ (wcmatch matchme
"*test[4-61]*")
T 

returns true because the string contains "test1".

The pattern string can specify multiple patterns, separated by commas. The following code returns T if matchme equals "ABC", or if it begins with "XYZ", or if it ends with "end".

_$ (wcmatch matchme
"ABC,XYZ*,*end")
T