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

Chapter 5

Figure 5-19

Negated Character Classes

Negated character classes always attempt to match a character. So the following negated character class means “Match a character that is not in the range uppercase A through F.”

[^A-F]

Using that pattern, as follows, will match AG and AZ because each is an uppercase A followed by a character that is not in the range A through F:

A[^A-F]

The pattern will not match A on its own because, while the match for A succeeds, there is no match for the negated character class [^A-F].

136

Character Classes

Combining Positive and Negative Character Classes

Some languages, such as Java, allow you to combine positive and negative character classes.

The following example shows how combined character classes can be used. The problem definition is as follows:

Match characters A and D through Z.

An alternative way to express that notion is as follows:

Match characters A through Z but not B through D.

You can express that in Java by combining character classes, as follows:

[A-Z&&[^B-D]]

Notice the paired ampersands, which means logical AND. So the pattern means “Match characters that are in the range A through Z AND are not in the range B through D.”

A simple Java command-line program is shown in CombinedClass2.java:

import java.util.regex.*;

public class CombinedClass2{

public static void main(String args[]) throws Exception{

String TestString = args[0];

String regex = “[A-Z&&[^B-D]]”;

Pattern p = Pattern.compile(regex);

Matcher m = p.matcher(TestString);

String match = null;

System.out.println(“INPUT: “ + TestString); System.out.println(“REGEX: “ + regex); while (m.find())

{

match = m.group(); System.out.println(“MATCH: “ + match); } // end while

if (match == null){

System.out.println(“There were no matches.”);

}// end if

}// end main()

}

137

Chapter 5

Try It Out

Combined Character Classes

These instructions assume that you have Java 1.4 correctly installed and configured. This example demonstrates how to use combined character classes in Java:

1.Open a command prompt window, and at the command –line, type javac CombinedClass2.java to compile the source code.

2.Type java CombinedClass2.java “A C E G” to run the program and supply a test string

“A C E G”.

3.Inspect the results, as shown in Figure 5-20. Notice that A, E, and G are matches, but C is not a match.

Figure 5-20

How It Works

You supply a test string at the command line. The test string is assigned to the variable TestString:

String TestString = args[0];

A regular expression is assigned to the variable regex:

String regex = “[A-Z&&[^B-D]]”;

The regular expression is the combined character class described earlier.

The compile() method of the Pattern object is executed with the regex variable as its argument:

Pattern p = Pattern.compile(regex);

Next, the matcher() method of the Pattern object, p, is executed with the TestString variable as its argument:

Matcher m = p.matcher(TestString);

A new variable, match, is assigned the value null:

String match = null;

138