Lesson 9

Looking Ahead and Behind

All the expressions used thus far have matched text, but sometimes you may want to use expressions to mark the position of text to be matched (in contrast to the matched text itself). This involves the use of lookaround (the capability to look ahead and behind), which will be explained in this lesson.

Introducing Lookaround

Again, we’ll start with an example. You need to extract the title of a Web page; HTML page titles are placed between <title> and </title> tags in the <head> section of HTML code. Here’s the example:

Text

<head>
<title>Ben Forta's Homepage</title>
</head>

RegEx

<[tT][iI][tT][lL][eE]>.*<\/[tT][iI][tT][lL][eE]>

Result

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.