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

Chapter 13

6.Next, test the /e switch, which limits matching to the end of a line.

At the command line, enter the following command:

findstr /n /i /e “Low” Low.txt

7.Inspect the results, as shown in Figure 13-14.

Figure 13-14

Only one line is displayed. If you expected three lines to be displayed, take a closer look at the lines. On two lines, where low is the last alphabetic character sequence, there is a period character after that sequence. In other words, low isn’t at the end of the line. That is the reason those two lines don’t match successfully.

8.You can achieve the same effects using more conventional metacharacters. To match only at the beginning of a line, you can use the ^ metacharacter.

Type the following command at the command line:

findstr /n /i “^Low” Low.txt

9.Inspect the results. The lines that were displayed in Figure 13-13 are again displayed.

10.Finally, you can use the $ metacharacter to match only at the end of the line.

At the command line, type the following command:

findstr /n /i “Low$” Low.txt

11.Inspect the results. Only one line is displayed — the same one as in Figure 13-14. The period character at the end of two lines prevents a successful match for the pattern Low$.

Command-Line Switch Examples

This section looks at the effects of several of the findstr command-line switches. Some produce direct effects on regular expressions — for example, the /i switch causes matching to be carried out case insensitively.

The /v Switch

The /v switch causes only lines that do not match to be displayed. This can be useful when you want to test for data that fails to correspond to the standards you expect.

For example, if you know that parts listed in a parts-number inventory should all consist of three alphabetic characters followed by three numeric digits, it is straightforward to find lines where a malformed part number is present.

316

Regular Expressions Using findstr

The content of the test file, PartNums2.txt, is shown here:

ABC876

A2D993

AB2882

AEJ88

KHD945

HEW78R

H

As you work through the following example, assume that case-sensitive matching is needed.

Try It Out

The /v Switch

1.Open a command window, and navigate to the directory where PartNums2.txt is located.

2.At the command prompt, enter the following command:

findstr /n /v “[A-Z][A-Z][A-Z][0-9][0-9][0-9]” PartNums2.txt

3.Inspect the results, as shown in Figure 13-15. Notice that several lines are displayed, which have supposed part numbers that do not match the pattern [A-Z][A-Z][A-Z][0-9][0-9][0-9].

Figure 13-15

4.To confirm that all lines have either matched or failed to match, you can run findstr again, omitting the /v switch.

At the command prompt, type the following command:

findstr /n “[A-Z][A-Z][A-Z][0-9][0-9][0-9]” PartNums2.txt

5.Inspect the results, as shown in Figure 13-16. Compare Figure 13-15 and Figure 13-16, and you will see that all lines appear in one or the other window, but no line is displayed in both.

Figure 13-16

317

Chapter 13

How It Works

When the /v switch is used, several blank lines are displayed. Because none of those lines contains the desired pattern [A-Z][A-Z][A-Z][0-9][0-9][0-9], there is no basis for a match. They are, therefore, displayed as lines not containing a match.

On Line 3, the text A2D993 does not match because the second character is a numeric digit, which does not match the [A-Z] character class that is second in the regular expression pattern.

On Line 5, the text AB2882 does not match because the third character is a numeric digit, which does not match the [A-Z] character class that is third in the regular expression pattern.

On Line 7, the text ABJ88 does not match because there are only two numeric digits and, therefore, no match for the third [0-9] character class.

On Line 11, the text HEW78R does not match because the sixth character is an uppercase R, which does not match the character class [0-9].

Turning to Step 4 and the results shown in Figure 13-16, lines 1 and 9 match because each contains a part number consisting of three uppercase alphabetic characters followed by three numeric digits, which matches the pattern [A-Z][A-Z][A-Z][0-9][0-9][0-9].

The /a Switch

The /a switch is followed by a colon and either one or two hexadecimal numbers. If a single hexadecimal number is used, that controls the text (or foreground) color for the information about line numbers and filenames returned by findstr. If two hexadecimal numbers are used, the first specifies the background color, and the second specifies the text color.

Hexadecimal Number

Color Specified

 

 

0

Black

1

Blue

2

Green

3

Aqua

4

Red

5

Purple

6

Yellow

7

White

8

Gray

9

Light blue

A

Light green

 

 

318