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

PowerGREP

Figure 14-3

Try It Out

Replace Tab

1.Click the Replace Tab once. The screen should appear as shown in Figure 14-3.

2.In the Replace text area, type the text regex, and click the Preview button.

3.Inspect the appearance in the results pane, as shown in Figure 14-4. If you view this on-screen, the original text is by default displayed in yellow, and the potential replacement text is displayed in green. In Line 3, for example, the original text, regular expression, is highlighted in yellow, and the potential replacement text, regex, is highlighted in green.

Figure 14-4

The File Finder Tab

The File Finder tab allows you to search for files in a chosen folder that contain a specified regular

expression.

329

Chapter 14

Immediately after running the Replace-tab example, the appearance in the File Finder tab is as shown in Figure 14-5. The results in the result pane are not those you would expect in the File Finder tab’s result pane. Data carries over from the previous activity.

Figure 14-5

Simply click the Find Files button, and the results appropriate to the File Finder tab are displayed, as you can see in Figure 14-6.

Figure 14-6

In this case, there is only a single file, Regex.txt, which contains matches to the specified regular expression reg(ular expression|(ex)p?).

Syntax Coloring

I will briefly mention one configuration option in PowerGREP that I find useful to turn off. By default, PowerGREP applies syntax coloring to regular expressions. Some of the screen shots in this chapter have syntax coloring turned on. However, as regular expressions become complex, I find the coloring can become confusing.

330

PowerGREP

To turn off syntax coloring, click the Preferences icon in the PowerGREP toolbar. On the Search Boxes tab, uncheck the Apply Syntax Coloring to Regular Expressions check box. Now regular expressions will be displayed as plain text.

Other Tabs

The Collect and Sequence tabs allow other uses of regular expressions but won’t be described further in this chapter. The Results tab simply displays the results information that is also displayed in the results pane of other tabs. The Undo History tab, depending on backups made, allows changes to be undone when carrying out replacements. Again, this facility won’t be considered further in this chapter. But these additional facilities give you a very powerful tool to apply regular expressions without your having to be familiar with the programming languages described later in this book.

Metacharacters Suppor ted

Compared to Microsoft Word, PowerGREP has vastly more support for regular expressions. It also has more supported options than either OpenOffice.org Writer or the findstr utility. Its regular expression support is comparable to the Komodo Regular Expressions Toolkit. PowerGREP has the advantage that you can use it as a utility to apply regular expressions to achieve practical tasks without programming.

The following table summarizes the metacharacters that are supported in PowerGREP. Most of the metacharacters listed are described further or used in examples later in this section or later in the chapter.

Metacharacter

Description

 

 

. (the dot character)

Matches almost any character.

\w

Matches an alphabetic character, numeric digit, and the underscore

 

character.

\W

Matches any character except alphabetic characters, numeric digits,

 

and the underscore character.

\d

Matches a numeric digit.

?

Quantifier; matches if the character or chunk that it qualifies occurs

 

zero or one time.

*

Quantifier; matches if the character or chunk that it qualifies occurs

 

zero or more times.

+

Quantifier; matches if the character or chunk that it qualifies occurs

 

one or more times.

{n,m}

Quantifier; matches if the character or chunk that it qualifies occurs a

 

minimum of n and a maximum of m times.

{n,}

Quantifier; matches if the character or chunk that it qualifies occurs a

 

minimum of n times. Maximum occurrences are unbounded.

 

Table continued on following page

331

Chapter 14

Metacharacter

Description

 

 

[ ...]

Character classes are supported.

[^ ...]

Negated character classes are supported.

( | )

Alternation is supported.

\1 etc

Back references are supported.

^

Positional metacharacter. Matches the position before the first charac-

 

ter on a line.

$

Positional metacharacter. Matches the position after the last character

 

on a line.

\b

Word-boundary position.

\<

Beginning-of-word position. Not supported.

\>

End-of-word position. Not supported.

 

 

Numeric Digits and Alphabetic Characters

PowerGREP supports the \w, \W, and \d metacharacters.

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

This line contains numbers, 1 2 3, and text . . . Blah, blah.

ABC

DEF 890

The next line has nonalphabetic characters.

?!ӣ$%^&*()_

1234567890

Try It Out

Matching Numeric Digits and Alphanumeric Characters

1.Open PowerGREP, and navigate to the Search tab. Ensure that the Regular Expression check box is checked.

2.In the Search text area, enter the simple regular expression \w.

3.In the Folder text box, type C:\BRegExp\Ch14, assuming that you downloaded the code file to the C: drive and unzipped it into the BRegExp directory. Adjust accordingly if you downloaded and unzipped it to another location.

4.In the File Mask text box, type AlphNumTest.txt, and inspect the results, as shown in Figure 14-7. As you can see, alphabetic characters, numeric digits, and the underscore character are matched by the \w metacharacter. Each match is a single character or digit.

332