Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
101
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Chapter 8

Why You Need Lookahead and Lookbehind

Chapter 1 looked at a fairly clumsy first attempt to modify one of the documents of the fictional Star Training Company. One of the problems in that first attempt at text replacement was an inability to express an idea such as “Match the word Star only when it is followed by the word Training.” That notion is lookahead.

As a further attempt at making the replacement of text in the Star Training Company documents more specific, you could create the following problem definition:

Match the sequence of Characters S, t, a, and r when it is followed by a space character and also followed by the sequence of characters T, r, a, i, n, i, n, and g.

In other words, you would match the word Star only when it is followed, with an intervening space character, by the word Training. This allows matching to be much more specific than any of the techniques used in earlier chapters.

The following pattern implements the preceding problem definition:

Star(?= Training)

Strictly speaking, when using the preceding pattern, you are matching Star when it is followed by a space character and the sequence of characters Training. If you want to ensure that only the word Training is in the lookahead, you can include the \b word boundary or the \> end-of-word metacharacter, depending on which metacharacters your tool supports:

Star(?= Training\b)

The characters that follow the (?= and precede the ) are not captured.

The (? metacharacters

Several metacharacters can be considered special kinds of opening parentheses, each of which begins with the sequence of characters (?. Several of those are briefly described here, because many developers find them fairly difficult to distinguish, and it may help you grasp which combination does what by seeing them together.

In Chapter 7, you saw the (?: form, which carries the idea of non-capturing grouping. The other special opening parentheses that apply to lookahead and lookbehind are described in this chapter. together with several examples of how they are used.

The combination (?= is used for positive lookahead. The syntax for positive lookahead, negative lookahead, positive lookbehind, and negative lookbehind is summarized in the following table for easy comparison.

196