Lesson 1. introducing regular expressions 2

How Regular Expressions Are Used
Look at the problem scenarios again and you will notice that they all fall into one of two types: Either information is being located (search) or information is being located and edited (replace). In fact, at its simplest, that is all that regular expressions are ever used for: search and replace. Every regular expression either matches text (performing a search) or matches and replaces text (performing a replace).

RegEx Searches
Regular expressions are used in searches when the text to be searched for is highly dynamic, as in searching for car in the scenario described earlier. For starters, you need to locate car or CAR or Car or even CaR; that’s the easy part (many search tools are capable of performing searches that are not case sensitive). The trickier part is ensuring that scar, carry, and incarcerate are not matched. Some more sophisticated editors have Match Only Whole Word options, but many don’t, and you may not be making this change in a document you are editing. Using a regular expression for the search, instead of just the text car, solves the problem.


Want to know what the solution to this one is? You’ve actually seen it already – it is the sample statement shown previously, b[Cc][Aa][Rr]b.

It is worth noting that testing for equality (for example, does this user-specified email address match this regular expression?) is a search operation. The entire user-provided string is being searched for a match (in contrast to a substring search, which is what searches usually are).

RegEx Replaces
Regular expression searches are immensely powerful, very useful, and not that difficult to learn. As such, many of the lessons and examples that you will run into are matches. However, the real power of regex is seen in replace operations, such as in the earlier scenario in which you replace URLs with clickable URLs. For starters, this requires that you be able to locate URLs within text (perhaps searching for strings that start with http:// or https:// and ending with a period or a comma or whitespace). Then it also requires that you replace the found URL with two occurrences of the matched string with embedded HTML so that

Is replaced with

The Search and Replace option in most applications could not handle this type of replace operation, but this task is incredibly easy using a regular expression.

Lesson 1. introducing regular expressions 2