Pattern Matching and Regular Expressions

See Also169S7QM

The pattern matching used in the Find and Replace commands on the Code menu is implemented using regular expressions.  A regular expression is a set of characters that describes how to match a text string being searched for.  In its simplest form, a regular expression is a character-for-character match of the string you're trying to find.  In Visual Basic, you can use special characters (often called metacharacters) to make your text searches more versatile.  Regular expressions work much like the wildcard characters "*" and "?" that the operating system recognizes, but with a few enhancements.

The special characters and the way they are used is described below.

Expression      Description


*                       Character wildcard.  Matches zero or more characters.  Must be enclosed in [ ] to match itself.  For example, a*a matches aa, a3a, aBa, and aBBBa.

?                      Character wildcard.  Matches any one character.  Must be enclosed in [ ] to match itself.  For example, a?a matches aaa, a3a, and aBA, but not aBBBa.

#                      Numeric wildcard.  Matches any one character from 0 through 9.  Must be enclosed in [ ] to match itself.  For example, a#a matches a0a, a1a, a2a, but not aaa or a10a.

[class ]             Character class.  Matches any one character in the class.  You can specify ranges by separating starting and ending characters with a hyphen.  For example, [a-z,0-9] matches any alphanumeric character.  Because the pattern matching is not case-sensitive, a-z works for both lowercase and uppercase letters.

                        A class can contain multiple clauses, each separated by a comma.  Each clause can consist of a single character or a range of characters.  For example, [D-Z,0-9] matches any letters and numbers but A, B, and C.

[!class]             Not character class.  Matches any character not specified in the class.  For example, [!a-z] matches anything but letters; [!0-9] matches anything but numbers.  You can use the exclamation point (!) only in the first character position of a class specification.  Other rules are the same as those in the character class.

See Also


Find, Find Next, Find Previous Commands (Edit Menu)112PA4A