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

Regular Expressions in StarOffice/OpenOffice.org Writer

Figure 12-15

POSIX Character Classes

In addition to support for conventional regular expression character classes, OpenOffice.org Writer version 1.1 supports a subset of the POSIX character classes. The supported classes and their interpretation are listed in the following Table. The ? character is part of the POSIX character class syntax. It is not a quantifier indicating that a preceding character class is optional.

Character Class

Meaning

 

 

[:digit:]?

Matches a single numeric digit when used alone. When used as part of a

 

longer pattern, it matches an optional numeric digit.

[:digit:]*

Matches zero or more numeric digits.

[:space:]?

Finds space characters.

 

Table continued on following page

301

Chapter 12

Character Class

Meaning

[:print:]? Matches a single character that prints, including space characters. When used as part of a longer pattern. it matches an optional printable character.

[:alnum:]? Matches a single alphabetic character or a numeric digit. As part of a longer pattern, it matches an optional alphanumeric character.

[:alpha:]? Matches an alphabetic character but not a numeric digit.

[:lower:]? Matches a lowercase character if the Match Case check box is checked. Otherwise, it behaves as [:alpha:]?.

[:upper:]? Matches an uppercase character if the Match Case check box is checked. Otherwise, it behaves as [:alpha:]?.

Matching Numeric Digits

As mentioned in the preceding table, the POSIX have some idiosyncracies when used alone. The example in this section walks you through a test file to clarify how the POSIX [:digit:]? character class behaves in OpenOffice.org Writer.

The test text is contained in the test file ADigitsB.txt, whose content is shown here:

A123B

AB

A8B

A1234567890B

As you can see, all the test strings have an uppercase A, followed by zero or more numeric digits, followed by an uppercase B.

Try It Out

The [:digit:] POSIX Character Class

1.Open the file ADigitsB.txt in OpenOffice.org Writer.

2.Open the Find & Replace dialog box using the Ctrl+F keyboard shortcut.

3.Check the Regular Expressions check box.

4.In the Search For text box, type the pattern [:digit:]?.

5.Click the Find button (not the Find All) button several times, each time inspecting the character that is highlighted.

You should see that only a single numeric digit is highlighted each time. When used alone, the pattern [:digit:]? matches exactly one numeric digit. The standalone pattern [:digit:] is not recognized by OpenOffice.org Writer; you can test this by deleting the ? in the pattern.

6.Click before the first character of the test file. Edit the pattern in the Search For text box to

[:digit:].

302

Regular Expressions in StarOffice/OpenOffice.org Writer

7.Click the Find button once, and observe the result.

You should see the dialog box shown in Figure 12-16. The message indicates that OpenOffice.org Writer has searched the entire document and found no match.

The quantifiers * and + produce the same matches when they are used alone. Each pattern [:digit:]* and [:digit:]+ matches one or more numeric digits.

8.Click before the first character of the file. Edit the regular expression pattern to [:digit:]*.

9.Click the Find button several times, inspecting the highlighted characters, until the end of the document is reached.

10.Modify the regular expression pattern to [:digit:]+.

11.Click the Find button several times, inspecting the highlighted characters, until the end of the document is reached.

12.When used in a longer pattern, the pattern [:digit:] behaves in slightly different ways. Click before the first character of the file. Edit the regular expression pattern to A[:digit:]B.

13.Click the Find button twice, each time observing the result. In this situation, [:digit:] behaves as you might have expected it to earlier — it matches exactly one numeric digit. Similarly, when [:digit:] forms part of a longer pattern, the ? quantifier operates as an indicator that the character class is optional.

Figure 12-16

303