Lesson 8

Using Backreferences

The previous lesson introduced subexpressions as a way to group characters into sets. A primary use of this type of grouping is to be able to properly control repeating pattern matches (as was demonstrated in that lesson). This lesson looks at the other important use of subexpressions—working with backreferences.

Understanding Backreferences

The best way to understand the need for backreferences is to look at an example. HTML developers use the header tags (<h1> through <h6>, with corresponding end tags) to define and format header text within Web pages. Suppose you needed to locate all header text, regardless of header level. Here’s the example:

Text

<body> <h1>Welcome to my Homepage</h1> ...

Get Learning Regular Expressions, First Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.