What Is Grep or Pattern Searching?

A grep pattern, also known as a regular expression, describes the text that you are looking for. For instance, a pattern can describe words that begin with C and end in l. A pattern like this would match "Call", "Cornwall", and "Criminal" as well as hundreds of other words.

In fact, you have probably already used pattern searching without realizing it. The Find dialog box's "Match Case" and "Entire Word" options turn on special searching patterns. Suppose that you are looking for "corn". With the "Match Case" option turned off, you are actually looking for a pattern that says: look for a C or c, O or o, R or r, and N or n. With the "Entire Word" option on, you are looking for the string "corn" only if it's surrounded by white space or punctuation characters; special search characters, called metacharacters, are added to the search string you specified to indicate this.

What makes pattern searching counterintuitive at first is how you describe the pattern. Consider the first example above, where we want to search for text that begins with the letter "C" and ends with the letter "l" with any number of letters in between. What exactly do you put between them that means "any number of letters"? That's what this chapter is all about.

Note: Grep is the name of a frequently-used Unix command that searches using regular expressions, the same type of search pattern used by BBEdit. For this reason, you will often see regular expressions called "grep patterns", as BBEdit does. They're the same thing.

Recommended Books and Resources

Mastering Regular Expressions by Jeffrey E.F. Friedl. O'Reilly & Associates, 1997. ISBN 1-56592-257-3 Mastering Regular Expressions is an outstanding resource for learning the "how-to" of writing useful grep patterns.