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

PowerGREP

The overall effect is that if Star occurs before zero or more spaces and then the character sequence Training, it is replaced by the character sequence Moon.

Alternation

Alternation is supported by PowerGREP. An example was shown in the first example in this chapter, using Regex.txt as a sample file.

Line Position Metacharacters

PowerGREP supports both line-end positional metacharacters ^ and $. The ^ metacharacter matches the position before the first character in a line. The $ metacharacter matches the position after the last character in a line.

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

This is here.

Look at this.

This is a theatre.

Theatre is as stimulating as this.

Try It Out

Line Position Metacharacters

1.Open PowerGREP, and ensure that the Regular Expression check box is checked.

2.In the Search text area, type the pattern ^This.

3.Ensure that the Folder text box contains C:\BRegExp\Ch14 or adapt this path if you downloaded code to a different directory.

4.In the File Mask text box, type LineEndTest.txt; click the Search button; and inspect the results, as shown in Figure 14-14. Remember that the default matching in PowerGREP is case insensitive. Notice that the character sequence This is matched only at the beginning of a line.

Figure 14-14

339

Chapter 14

5.Modify the pattern in the Search text area to this\.$. Remember that if the period character is the last character on the line, the pattern this$ will never match with the test text for this example.

6.Click the Search button, and inspect the results, as shown in Figure 14-15. Both of the occurrences of the character sequence this. at the end of a line are matched.

Figure 14-15

How It Works

First, let’s look at matching with the pattern ^This. This matches the position before the first character on a line and then attempts to match the character sequence This. This means that it matches the character sequence This at the beginning of a line.

When the pattern is this\.$, this followed by a period character, followed by the $ metacharacter, the character sequence this followed by a period character is first matched; then an attempt is made to match the end-of-line position. That only succeeds when this. makes up the final five characters on a line.

Word-Boundary Metacharacters

Only the \b word-boundary character is supported in PowerGREP. Neither the \< nor the \> wordboundary metacharacters is supported.

The test text for this example, Cat.txt, is shown here:

Catalonia is a region of Spain.

“Scat,” he said.

A caterpillar later becomes a butterfly.

I love my cat.

The cat sat on the mat.

340

PowerGREP

Try it Out

Word-Boundary Position Metacharacters

1.Open PowerGREP, and ensure that the Regular Expression check box is checked.

2.In the Search text area, type the pattern \bcat.

3.In the Folder text box, type C:\BRegExp\Ch14, adjusting the folder if you downloaded files to some other location.

4.In the File Mask text box, type Cat.txt; click the Search button; and inspect the results, as shown in Figure 14-16. Notice that the character sequence cat is matched only when it is the first three characters of an alphabetic character sequence — colloquially, a word.

Figure 14-16

5.Modify the pattern in the Search text area to cat\b; click the Search button; and inspect the results, as shown in Figure 14-17. Notice that the character sequence cat is matched only when it is the last three characters of an alphabetic character sequence.

Figure 14-17

341

Chapter 14

6.Modify the pattern in the Search text area to \bcat\b; click the Search button; and inspect the results, as shown in Figure 14-18.

Figure 14-18

How It Works

The pattern \bcat first matches a boundary between nonalphabetic and alphabetic characters. Because the \b metacharacter is followed by a sequence of alphabetic characters, it is, in that context, functioning as a beginning-of-word position metacharacter. Therefore, \bcat matches the three-character sequence of characters cat when it occurs at the beginning of a word.

The pattern cat\b matches the three-character sequence of characters cat when the sequence is followed by a word-boundary position. Therefore, cat\b matches the character sequence when it occurs at the end of a word, as in cat or Scat.

The pattern \bcat\b matches only when there is a word-boundary position before the character sequence cat and immediately after it. In other words, the pattern \bcat\b matches only the word cat.

Lookahead and Lookbehind

Lookahead and lookbehind are supported in PowerGREP.

The following example uses the StarOriginal.txt file, which was used earlier in the chapter.

Try It Out

Lookahead and Lookbehind

1.Open PowerGREP, and ensure that the Regular Expression check box is checked.

2.In the Search text area, type the pattern Star(?=.).

3.In the Folder text box, type C:\BRegExp\Ch14 or adapt the path if you downloaded sample files to another location.

4.In the File Mask text box, type StarOriginal.txt; click the Search button; and inspect the results, as shown in Figure 14-19.

5.Edit the pattern in the Search text area to (?<=with )Star, click the Search button, and inspect the results.

342