O'Reilly logo

Learning Regular Expressions, First Edition by Ben Forta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required