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

Simple Regular Expressions

Figure 3-21

Exercises

These exercises allow you to test your understanding of the regular expression syntax covered in this chapter.

1.Using DoubledR.txt as a sample file, try out regular expression patterns that match other doubled letters in the file. For example, there are doubled lowercase s, m, and l. Use different syntax options to match exactly two occurrences of a character.

2.Create a regular expression pattern that tests for part numbers that have two alphabetic characters in sequence — uppercase A followed by uppercase B followed by two numeric digits.

3.Modify the file UpperL.html so that the regular expression pattern to be matched is the. Open the file in a browser, and test various pieces of text against the specified regular expression pattern.

71

4

Metacharacters and

Modifiers

This chapter moves on to look at several regular expression metacharacters and modifiers. Metacharacters can be combined with literal characters and quantifiers, which were discussed in Chapter 3, to create more complex regular expression patterns. Using metacharacters allows you to release more of the power and flexibility of regular expressions.

A metacharacter is a character that is used to convey a meaning other than itself. For example, the period character (also called a full stop) is a metacharacter that can signify any alphanumeric

character — that is, any uppercase or lowercase character used in English or any alphabetic character used in other languages or any numeric digit 1 through 9. Other regular expression metacharacters allow ASCII alphabetic characters and numeric digits to be specified separately. In addition, there are metacharacters that match whitespace characters, such as the space character, or other invisible characters, such as line feeds.

This chapter does not attempt to cover all metacharacters. Several metacharacters — such as those that signify the beginning and end of lines (^ and $), the beginning and end of words (\< and \>), and word boundaries (\b) — are described and demonstrated in Chapter 6. The metacharacters considered in Chapter 6 signify position. The metacharacters described in this chapter signify classes of characters.

A modifier, not surprisingly, modifies how a regular expression is applied. Depending on the language or tool being used, there are modifiers to specify whether a regular expression pattern is to be interpreted in a case-sensitive or case-insensitive way and how lines or paragraphs are to be handled.

The following metacharacters are introduced in this chapter:

The . metacharacter

The \w and \W metacharacters

The \d and \D metacharacters

Metacharacters that match whitespace characters, such as the space character