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

Regular Expressions in Java

How It Works

The test string is assigned to the myTestString variable:

String myTestString = “12345-1234 23456 45678 01234-1234”;

A pattern that can match U.S. Zip codes is assigned to the myRegex variable:

String myRegex = “\\d{5}(-\\d{4})*”;

The myRegex and myTestString variables are used in the creation of a Pattern object, myPattern, and a Matcher object, myMatcher:

Pattern myPattern = Pattern.compile(myRegex);

Matcher myMatcher = myPattern.matcher(myTestString);

The empty string is assigned to the variable myMatch. This will allow you to test later whether there has been a match or not. If the value of myMatch is still the empty string, there has been no match:

String myMatch = “”;

The original values of the test string, myTestString, and the regular expression pattern, myRegex, are displayed:

System.out.println(“The test string was ‘“ + myTestString + “‘.”);

System.out.println(“The pattern was ‘“ + myRegex + “‘.”);

A while loop uses the find() method as a test to determine whether or not a match has been found. If there is no match at all, the code in the while loop is never executed. For each match, the value of the match is displayed. After the final match, the test myMatcher.find() returns false, so the while loop exits:

while (myMatcher.find())

{

myMatch = myMatcher.group();

System.out.println(“The match ‘“ + myMatch + “‘ was found.”); } // end while

If the value of myMatch is still the empty string, there has been no match, so you can safely output a message indicating that there was no match. If there has been a match, the value of the myMatch variable will be something other than the empty string, and the following message will not be displayed:

if (myMatch == “”){

System.out.println(“There were no matches.”); } // end if

Character Classes

Support for character classes in java.util.regex includes the character classes, negated character classes, and character class ranges (discussed in the introductory chapters of this book). However, there is some useful additional functionality in java.util.regex that is unsupported in many regular expression implementations.

647

Chapter 25

The following example demonstrates basic character class functionality.

Try It Out

Character Class

1.Type the following code in a text editor:

import java.util.regex.*;

public class CharClass{

public static void main(String args[]){ findMatches(args[0]);

} // end main()

public static boolean findMatches(String testString){ String myRegex = “[A-D]\\d”;

Pattern myPattern = Pattern.compile(myRegex);

Matcher myMatcher = myPattern.matcher(testString);

String myMatch = null;

System.out.println(“The test string was: “ + testString); System.out.println(“The regular expression pattern was:” + myRegex); while (myMatcher.find())

{

myMatch = myMatcher.group(); System.out.println(“Match found: “ + myMatch); } // end while

if (myMatch == null){ System.out.println(“There were no matches.”);

} // end if

return true;

} // findMatches()

}

2.Save the code as CharClass.java.

3.Compile the code. At the command line, type javac CharClass.java.

4.Run the code. At the command line, type java CharClass “A1 B2 C3 D9 E8 F3 G5”, and inspect the results, as shown in Figure 25-9. Notice that the character sequences E8, F3, and G5 do not match.

Figure 25-9

648

Regular Expressions in Java

How It Works

The myRegex variable is assigned the pattern [A-D]\d:

String myRegex = “[A-D]\\d”;

The test string A1 B2 C3 D9 E8 F3 G5 is input at the command line and is assigned to the args[0] array element, which is used as the findMatches() method’s argument:

findMatches(args[0]);

A while loop is used to display each match that is found. While the test for the while statement returns true, the code in the while loop is executed. When there are no more matches, the find() method returns the boolean value false, and the while loop exits:

while (myMatcher.find())

{

myMatch = myMatcher.group(); System.out.println(“Match found: “ + myMatch); } // end while

Java supports the use of unions of character classes, as demonstrated in the following example.

Try It Out

Union of Character Classes

1.Type the following code in a text editor:

import java.util.regex.*;

public class CharClassUnion{

public static void main(String args[]){ findMatches(args[0]);

} // end main()

public static boolean findMatches(String testString){ String myRegex = “[A-D[H-M]]\\d”;

Pattern myPattern = Pattern.compile(myRegex);

Matcher myMatcher = myPattern.matcher(testString);

String myMatch = null;

System.out.println(“The test string was: “ + testString); System.out.println(“The regular expression pattern was:” + myRegex); while (myMatcher.find())

{

myMatch = myMatcher.group(); System.out.println(“Match found: “ + myMatch); } // end while

if (myMatch == null){ System.out.println(“There were no matches.”); } // end if

649

Chapter 25

return true;

} // findMatches()

}

2.Save the code as CharClassUnion.java.

3.Compile the code. At the command line, type javac CharClassUnion.java.

4.Run the code. At the command line, type java CharClassUnion “A1 B2 C3 D4 E5 H9 I2 J3 N4”, and inspect the results, as displayed in Figure 25-10. Notice that the character sequences E5 and N4 are not matched.

Figure 25-10

How It Works

The string assigned to the myRegex variable specifies the union of two character classes, [A-D] and [H-M]. The union operator is implicit. The character class is equivalent to [A-DH-M]:

String myRegex = “[A-D[H-M]]\\d”;

Each match is displayed using the while loop as previously described.

If the alphabetic character is in the character class [A-DH-M], together with the following numeric digit, there is a match.

Try It Out

Subtraction of Character Classes

1.Type the following code in a text editor:

import java.util.regex.*;

public class CharClassSubtraction{ public static void main(String args[]){ String testString = args[0]; findMatches(testString);

} // end main()

public static boolean findMatches(String testString){ String myRegex = “[A-Z&&[^H-M]]\\d”;

Pattern myPattern = Pattern.compile(myRegex);

Matcher myMatcher = myPattern.matcher(testString);

String myMatch = null;

650