Finding Strings with Regular Expressions Before you can manipulate a string, you first must find it. Although some programming languages include string searching functions, most of them are fairly limited to finding exact matches of strings. To remedy this problem, many programming languages (such as Perl and Tcl) use regular expressions. (A regular expression is just a series of symbols that tell the computer how to find a specific pattern in a string.) If a programming language doesn’t offer built-in support for regular expressions, many programmers have written subprogram libraries that let you add regular expressions to your program. By using regular expressions, your programs can perform more sophisticated text searching than any built-in string functions could ever do.


Pattern matching with the single character (.) wildcard The simplest way to search for a pattern is to look for a single character. For example, you might want to know if a certain string begins with the letter b, ends with the letter t, and contains exactly one character between. Although you could repetitively check every three-character string that begins with b and ends with t, like bat or but, it’s much easier to use a single-character wildcard instead, which is a dot or period character (.).


So if you want to find every three-letter string that begins with a b and ends with a t, you’d use this regular expression: b.t To search for multiple characters, use the (.) wildcard multiple times to match multiple characters. So the pattern b..t matches the strings boot and boat with the two (..) wildcards representing the two characters between the b and the t. Of course, the b..t pattern doesn’t match bat because bat has only one character between the b and the t. Nor does it match boost because boost has more than two characters between the b and the t. When using the (.) wildcard, you must know the exact number of characters to match.