XML technologies / Regular expressions / Introduction to regular expressions

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

Imagine being asked to search every page on a web server for doubled words. Such repetitions, like “das das”, often appear in repeatedly revised text. You must find a solution that:

Quite a list of requirements! But it is a real problem that needs solving. Running such a program on a version of this manuscript, I was surprised by the number of repetitions. Many programming languages could solve the task, but one supporting regular expressions makes it much easier.

Regular expressions are a powerful, flexible, efficient way to process text. More precisely, they are a general notation for describing text patterns, almost a small programming language for testing and manipulating text. Combined with a tool’s other facilities, they can expand, reduce, and otherwise transform all kinds of text. Uses range from a text editor’s search function to an entire text-processing language. The following pages show how regexes increase productivity by teaching how to think in regular expressions and thus exploit their full potential.

A complete solution may take just a few lines in a modern language supporting regexes. A single “search and replace” statement can find and highlight doubled words. Another can discard lines without repetitions, leaving only interesting lines. A third adds each line’s source file name. Extended introductory examples contain complete examples in Perl and Java.

The programming language, whether Perl, Java, VB.NET, or another, supplies the environment, but the real power comes from regular expressions. Learning to use that power lets you search and highlight text and discard uninteresting parts for your own purposes. Combine regexes with the language’s constructs to manipulate text, whether adding escape sequences or omitting or changing content.

  

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top