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

Chapter 4

Using the period metacharacter is acceptable here because you are testing user-entered text. As mentioned earlier, greedy matching can cause problems when using the period metacharacter because it can match many lines or pages of text.

The \S Metacharacter

The \S metacharacter matches any character that is not a whitespace character. Characters from languages other than English will also match the \S metacharacter. The \S metacharacter does not match quite as many characters as the period metacharacter (which matches all characters with the possible exception of the newline character, depending on settings).

The \t Metacharacter

The \t metacharacter matches a tab character.

It is not possible to use the Komodo Regular Expression Toolkit unless you paste in the text containing a tab character, because typing a tab character in the Enter a String to Match Against area causes the focus to move away from that area instead of adding a tab character to the area.

The sample file, Tabs.txt, contains words that are separated from one another by tab characters:

Words

separated

by

tabs.

 

Some

more

tab-separated

words.

Try It Out

The \t Metacharacter

 

1.Open OpenOffice.org Writer, and open the test file Tabs.txt in OpenOffice.org Writer.

2.Press Ctrl+F to open the Search & Replace dialog box, and check the Regular Expressions and Match Case check boxes.

3.Enter the pattern \t in the Search For text box; click the Find All button; and inspect the highlighted text that matches the pattern \t, as shown in Figure 4-18.

How It Works

The regular expression engine begins at the position before the W of Words. Each character is tested in turn to determine whether it matches the \t pattern — in other words, whether it is a tab character. Each tab character in the document is highlighted.

98

Metacharacters and Modifiers

Figure 4-18

The \n Metacharacter

The \n metacharacter matches a newline character — in other words, it matches the character that you add to a text file when you press the Return key.

Try It Out

The \n Metacharacter

1.Open the Komodo Regular Expression Toolkit, and clear any existing regular expression pattern and sample text.

2.In the Enter a String to Match Against area, enter the following test text:

Andrew

Watt

In other words, type Andrew, then press the Return key, then type Watt. The text in the Enter a String to Match Against area should look like the text shown in Figure 4-19.

99

Chapter 4

Figure 4-19

3.In the Enter a Regular Expression area, type \n.

4.Inspect the area below the Enter a String to Match Against area. The Komodo Regular Expression Toolkit should show Match succeeded: 0 groups. In addition, immediately after the final character on the first line of the test text, a whitespace character (the newline character) should be highlighted in pale green, indicating that the regular expression pattern has matched the newline character.

How It Works

The regular expression engine starts at a position before the A of Andrew and tests whether the character that follows is a newline character. If not, it moves to the position after the tested character and tests the character immediately following that new position. If that character is a newline character, there is a match. As mentioned earlier, in the Komodo Regular Expression Toolkit, a match is indicated by pale green highlighting. Because the newline character is an invisible character (other than the characters that follow it being on a new line), a seemingly blank character is highlighted.

Try It Out

The \n Metacharacter in Visual Studio

The \n metacharacter is also supported in the Microsoft Visual Studio 2003 editor. If you have a copy of Visual Studio 2003, you can work through the following example:

1.Open Microsoft Visual Studio 2003, and open the Person2.xml test file.

2.Use Ctrl+F to open the Find window, and check the Use check box.

3.In the drop-down list toward the bottom-left corner of the Find window, select the Regular Expressions option.

100

Metacharacters and Modifiers

4.Enter the pattern \n in the Find What text box, and click the Find Next button.

5.Inspect the highlighting of the first match. If the cursor was initially at the beginning of the file, the first match for the pattern \n should occur at the end of the first line.

Figure 4-20 shows the appearance in the Visual Studio 2003 editor after following the preceding steps.

Figure 4-20

How It Works

With the cursor at the beginning of the file Person2.xml, the regular expression engine in Visual Studio 2003 checks each character in turn to see if it matches the pattern \n — in other words, whether it is a newline character. The first matching character is highlighted, as shown at the end of the first line in the sample file.

101