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:
<BODY><H1>Welcome to my Homepage</H1>Content is divided into two sections:<BR> ...
Get Sams Teach Yourself Regular Expressions in 10 Minutes 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.