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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.