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

PowerGREP

Figure 14-7

5.Edit the pattern in the Search area to \d, and inspect the results. You will see that the \d metacharacter matches the numeric digits in the sample file, each match being a single numeric digit.

Quantifiers

PowerGREP supports the ?, * and + quantifiers, as well as the {n,m} syntax.

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

AB123DEF

AB1DEF

ABDEF

AB12DEF

AB1234567890DEF

Notice that each line has the character sequence AB, followed by zero or more numeric digits, followed by the character sequence DEF.

Try It Out

Quantifiers

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

2.In the Search text area, type AB\d?DEF.

333

Chapter 14

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 ABDEF.txt, click the Search button, and inspect the results. The text that matches is ABDEF (zero occurrences of a numeric digit) and AB1DEF (one occurrence of a numeric digit).

5.Edit the Search text area content to read AB\d*DEF, click the Search button, and inspect the results, as shown in Figure 14-8.

Notice that all lines in the test text are matched because each has the character sequence AB, followed by zero or more numeric digits, followed by the character sequence DEF.

6.Edit the content of the Search text area to read AB\d+DEF, click the Search button, and inspect the results.

The + quantifier matches one or more occurrences. Therefore, the former match ABDEF (on Line 5 in Figure 14-8) no longer matches because it has zero occurrences of a numeric digit. The other lines in Figure 14-8 continue to match because they contain one or more occurrences of a numeric digit.

7.Edit the content of the Search text area to read AB\d{0,3}DEF, click the Search button, and inspect the results.

Notice in Figure 14-9 that there is no match on Line 9, because AB1234567890DEF contains 10 numeric digits, and the maximum the pattern AB\d{0,3}DEF will match is three.

8.Edit the content of the Search text box to read AB\d{2,}DEF, click the Search button, and inspect the results.

The matching text is AB123DEF, AB12DEF, and AB1234567890DEF. Each match has a minimum of two occurrences of a numeric digit. The maximum number of permitted occurrences is unbounded.

Figure 14-8

334

PowerGREP

Figure 14-9

Back References

PowerGREP supports the use of back references. Each pair of parentheses in a regular expression creates a group. Each group captured by parentheses in the regular expression is captured in numerical order and can be referenced by using \1, \2, and so on.

The following example uses PowerGREP and back references to replace all occurrences of Star in the term Star Training with Moon. For convenience, the test file StarOriginal.txt, is reproduced here. Take careful note of each occurrence of Star Training.

Star Training Company

Starting from May 1st Star Training Company is offering a startling special offer to our regular customers - a 20% discount when 4 or more staff attend a single Star Training Company course.

In addition, each quarter our star customer will receive a voucher for a free holiday away from the pressures of the office. Staring at a computer screen all day might be replaced by starfish and swimming in the Seychelles.

Once this offer has started and you hear about other Star Training customers enjoying their free holiday you might feel left out. Don’t be left on the outside staring in. Start right now building your points to allow you to start out on your very own Star Training holiday.

Reach for the star. Training is valuable in its own right but the possibility of a free holiday adds a startling new dimension to the benefits of Star Training training.

Don’t stare at that computer screen any longer. Start now with Star. Training is crucial to your company’s wellbeing. Think Star.

You replace Star only when it precedes Training.

335

Chapter 14

Try It Out

Replacement Using Back References

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

2.In the Search text area, type (Star)( *)(Training).

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 StarOriginal.txt, and click the Preview button. Figure 14-10 shows the results. You may need to scroll the window horizontally to see all the matches. On-screen, the text that matches is shown in yellow. The potential replacement text is shown in green.

The suggested replacements look good. So we are in good shape to go ahead and commit to the replacement.

In PowerGREP terminology the “target” is a file after an attempted search-and-replace operation. There are many options, only some of which are demonstrated here.

5.In the first drop-down list in the Target section, select Copy Only Modified Files.

6.In the second drop-down list in the Target section, select Single Folder.

7.In the third text area drop-down list in the Target section, enter C:\BRegExp\Ch14\Changed. Alternatively, you can browse to a desired folder destination.

8.In the first drop-down list in the Backup section, select *.bak.

9.In the second drop-down list in the Backup section, select Single Folder.

10.In the third text area drop-down list in the Backup section, enter C:\BRegExp\Ch14\Changed. Figure 14-11 shows the appearance after this step.

Steps 5 to 7 place modified files into the C:\BRegExp\Ch14\Changed directory, giving them the same names as the originals.

Figure 14-10

336

PowerGREP

Steps 8 to 10 make a backup of the original file in the folder C:\BRegExp\Ch14\Changed and give it a .bak file extension. The files added to the Changed folder are shown in Figure 14-12.

Figure 14-11

The use of relative paths for targets and backups in PowerGREP can be quite confusing. If you attempt to use the . abbreviation, thinking that this will save a backup in the same directory as the original file, you will be disappointed. It will result in the files being saved in the directory into which PowerGREP is installed, rather than where you probably intended.

The safest technique is to avoid relative paths entirely and to use only absolute paths.

Figure 14-12

337

Chapter 14

The file, after replacement, is located at C:\BRegExp\Ch14\Changed\StarOriginal.txt. Its content is shown in the Komodo 2.5 editor (to show the full path) in Figure 14-13.

Figure 14-13

Notice that each of the occurrences of the character sequence Star, which were found in the Preview, has been replaced by the character sequence Moon.

How It Works

This section focuses only on how the back reference works. Explanation of the aspects that relate specifically to PowerGREP is found within the steps in the preceding Try It Out section.

The pattern to be matched is (Star)( *)(Training). This will match the character sequence Star, zero or more whitespace characters (one or more is likely in the test text), and the character sequence

Training.

The first component of the regular expression, (Star), matches the character sequence Star and also captures it into \1. You won’t use \1 in the replace part of the search and replace. The character sequence Moon is used in the replace where \1 occurred.

The second component of the regular expression, ( *), will match and capture zero or more space characters in \2. Any whitespace captured will be used in the replace exactly as it occurred in the original text. An alternative approach would have been to replace one or more space characters with a single literal space character.

The third component of the regular expression, (Training), matches and captures the character sequence Training in \3. The \3 in the replace simply copies the character sequence Training from the original to the changed text.

338