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

Simple Regular Expressions

Again, the myRegExp and entry variables are used to give feedback to the user about what is to be matched and the string that he or she entered.

Of course, in practice, you typically want to match a sequence of characters rather than a single character.

Matching Sequences of Characters That Each Occur Once

When the regular expression pattern L was matched, you made use of the default behavior of the regular expression engine, meaning that when there is no indication of how often a character (or sequence of characters) is allowed to occur, the regular expression engine assumes that the character(s) in the pattern occur exactly once, except when you include a quantifier in the regular expression pattern that specifies an occurrence other than exactly once. This behavior also allows the matching of sequences of the same character.

To match two characters that are the same character and occur twice without any intervening characters (including whitespace), you can simply use a pattern with the desired character written twice in the pattern.

Try It Out

Matching Doubled Characters

As an example, look at how you can match sequences of characters where a character occurs exactly twice — for example, the doubled r that can occur in words such as arrow and narrative.

A problem definition for the desired match can be expressed as follows:

Match any occurrence of the lowercase character r immediately followed by another lowercase r.

An example file, DoubledR.txt, is shown here:

The arrow flew through the air at great speed.

This is a narrative of great interest to many readers.

Apples and oranges are both types of fruit.

Asses and donkeys are both four-legged mammals.

Several million barrels of oil are produced daily.

The following pattern will match all occurrences of rr in the sample file:

rr

1.Open OpenOffice.org Writer, and open the sample file DoubledR.txt.

2.Use the keyboard shortcut Ctrl+F to open the Find and Replace dialog box.

3.Check the Regular Expressions check box and the Match Case check box.

4.Enter the pattern rr in the Search For text box, and click the Find All button.

47

Chapter 3

Figure 3-5 shows DoubledR.txt opened in OpenOffice.org Writer, as previously described. Notice that all occurrences of rr are matched, but single occurrences of r are not matched.

Figure 3-5

How It Works

The pattern rr indicates to the regular expression engine that an attempt should be made to match the lowercase alphabetic character r; then, if that first match is successful, an attempt should be made to match the next character. The entire match is successful if the second character is also a lowercase r.

If the attempt to match the first character fails, the next character is tested to see if it is a lowercase r. If it is not a lowercase r, the match fails, and a new attempt is made to match the following character against the first r of the regular expression pattern.

You can also try this out in the Komodo Regular Expression Toolkit, as shown in Figure 3-6, which matches successive lowercase ms. The latest trial version of the Komodo IDE, which includes the Regular Expression Toolkit, can be downloaded from http://activestate.com/Products/Komodo. Komodo version 2.5 is used in this chapter. Clear the regular expression and the test text from the Komodo Toolkit. Enter mammals in the area for the string to be matched, and type m in the area for the regular

48

Simple Regular Expressions

expression. At that point, the initial m of mammals is matched. Then type a second m in the area for the regular expression, and the highlight indicating a match moves to the mm in the middle of mammals, as you can see in Figure 3-6.

Figure 3-6

These two examples have shown how you can match doubled characters using one of the syntax options that are available. Later in this chapter, you will look at an alternative syntax that can match an exact number of successive occurrences of a desired character, which can be exactly two or can be a larger number. The alternative syntax uses curly braces and, in addition to allowing matches of an exact number of occurrences, allows variable numbers of occurrences to be matched.

Introducing Metacharacters

To match three characters, you can simply write the character three times in a row to form a pattern. For example, to match part numbers that take the form ABC123 (in other words, three alphabetic characters followed by three numeric digits, which will match the alphabetic characters AAA), simply use the following pattern:

AAA

To match the other part of such part numbers, you need to introduce the concept of a metacharacter. The patterns you have seen so far include characters that stand, literally, for the same character. A metacharacter can be a single character or a pair of characters (the first is typically a backslash) that has a meaning other than the literal characters it contains.

There are several ways in which you can match the 123 part of a part number of the form ABC123. One is to write the following:

\d\d\d

Each \d is a metacharacter that stands for a numeric digit 0 through 9, inclusive. The \d metacharacter does not stand for a backslash followed by a lowercase d.

49

Chapter 3

Notice that the \d metacharacter differs significantly in meaning from the literal characters we have used in patterns so far. The character L in a pattern could match only an uppercase L, but the metacharacter \d can match any of the numeric digits 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.

A metacharacter often matches a class of characters. In this case, the metacharacter \d matches the class of characters that are numeric digits.

When you have the pattern \d\d\d, you know that it matches three successive numeric digits, but it will match 012, 234, 345, 999 and hundreds of other numbers.

Try It Out

Matching Triple Numeric Digits

Suppose that you want to match a sequence of three numeric digits. In plain English, you might say that you want to match a three-digit number. A slightly more formal way to express what you want to do is this: Match a numeric digit. If the first character is a numeric digit, attempt to match the next character as a numeric digit. If both the characters are numeric digits, attempt to match a third successive numeric digit.

The metacharacter \d matches a single numeric digit; therefore, as described a little earlier, you could use the pattern:

\d\d\d

to match three successive numeric digits.

If all three matches are successful, a match for the regular expression pattern has been found.

The test file, ABC123.txt, is shown here:

ABC123

A234BC

A23BCD4

Part Number DRC22

Part Number XFA221

Part Number RRG417

For the moment, let’s aim to match only the numeric digits using the pattern \d\d\d shown earlier.

For this example, we will use JavaScript, for reasons that will be explained in a moment.

1.Navigate to the directory that contains the file ABC123.txt and ThreeDigits.html. Open

ThreeDigits.html in a Web browser.

2.Click the button labeled Click Here to Enter Text.

3.When the prompt box opens, enter a string to test. Enter a string copied from ABC123.txt.

4.Click the OK button and inspect the alert box to see if the string that you entered contained a match for the pattern \d\d\d.

50

Simple Regular Expressions

Figure 3-7 shows the result after entering the string Part Number RRG417.

Figure 3-7

Try each of the strings from ABC123.txt. You can also create your own test string. Notice that the pattern \d\d\d will match any sequence of three successive numeric digits, but single numeric digits or pairs of numeric digits are not matched.

How It Works

The regular expression engine looks for a numeric digit. If the first character that it tests is not a numeric digit, it moves one character through the test string and then tests whether that character matches a numeric digit. If not, it moves one character further and tests again.

If a match is found for the first occurrence of \d, the regular expression engine tests if the next character is also a numeric digit. If that matches, a third character is tested to determine if it matches the \d metacharacter for a numeric digit. If three successive characters are each a numeric digit, there is a match for the regular expression pattern \d\d\d.

You can see this matching process in action by using the Komodo Regular Expressions Toolkit. Open the Komodo Regular Expression Toolkit, and clear any existing regular expression and test string. Enter the test string A234BC; then, in the area for the regular expression pattern, enter the pattern \d. You will see that the first numeric digit, 2, is highlighted as a match. Add a second \d to the regular expression area, and you will see that 23 is highlighted as a match. Finally, add a third \d to give a final regular expression pattern \d\d\d, and you will see that 234 is highlighted as a match. See Figure 3-8.

You can try this with other test text from ABC123.txt. I suggest that you also try this out with your own test text that includes numeric digits and see which test strings match. You may find that you need to add a space character after the test string for matching to work correctly in the Komodo Regular Expression Toolkit.

Why did we use JavaScript for the preceding example? Because we can’t use OpenOffice.org Writer to test matches for the \d metacharacter.

51

Chapter 3

Figure 3-8

Matching numeric digits can pose difficulties. Figure 3-9 shows the result of an attempted match in ABC123.txt when using OpenOffice.org Writer with the pattern \d\d\d.

Figure 3-9

52

Simple Regular Expressions

As you can see in Figure 3-9, no match is found in OpenOffice.org Writer. Numeric digits in OpenOffice.org Writer use nonstandard syntax in that OpenOffice.org Writer lacks support for the \d metacharacter.

One solution to this type of problem in OpenOffice.org Writer is to use character classes, which are described in detail in Chapter 5. For now, it is sufficient to note that the regular expression pattern:

[0-9][0-9][0-9]

gives the same results as the pattern \d\d\d, because the meaning of [0-9][0-9][0-9] is the same as \d\d\d. The use of that character class to match three successive numeric digits in the file ABC123.txt is shown in Figure 3-10.

Figure 3-10

Another syntax in OpenOffice.org Writer, which uses POSIX metacharacters, is described in Chapter 12.

The findstr utility also lacks the \d metacharacter, so if you want to use it to find matches, you must use the preceding character class shown in the command line, as follows:

findstr /N [0-9][0-9][0-9] ABC123.txt

53