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

Chapter 25

The matches() Method

The matches() method attempts to match a regular expression pattern against the whole of the test string. It returns a boolean value of true if the whole test string matches the regular expression pattern.

The pattern() Method

The pattern() method takes no argument. It returns a Pattern object. The Pattern object contains the regular expression pattern that the Matcher object uses in matching.

The replaceAll() Method

The replaceAll() method replaces every occurrence of a character sequence in the test string that matches the regular expression pattern with a specified replacement string. The replacement string is the replaceAll() method’s sole argument. The return value is a String.

Try It Out

Using the replaceAll() Method

1.Type the following code in a text editor:

import java.util.regex.*;

public class replaceAll{

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

} // end main()

public static boolean myReplace(String testString){

String myMatch = “Star”;

Pattern myPattern = Pattern.compile(myMatch);

Matcher myMatcher = myPattern.matcher(testString);

String testResult = myMatcher.replaceAll(“Moon”);

System.out.println(“The test string is: \n’” + testString + “‘.”);

System.out.println();

if (testResult.length() > 0)

{

System.out.println(“After replacement the string is: \n’” + testResult + “‘.” );

}

else

{

System.out.println(“No match was found.”);

}

return true;

} // end myReplace()

}

2.Save the code as replaceAll.java; compile the code; and at the command line, type javac replaceAll.java.

642

Regular Expressions in Java

3.Run the code. At the command line, type java replaceAll “Star training is great. Star Training Company is well known.” Inspect the results, as shown in Figure 25-7. Notice that each occurrence of the character sequence Star has been replaced by the character sequence Moon.

Figure 25-7

How It Works

The code in the main() method calls the myReplace() method, passing a string entered on the command line:

myReplace(args[0]);

In the myReplace() method, a literal pattern, Star, is assigned to the myMatch variable:

String myMatch = “Star”;

Then a Pattern object and a Matcher object are created:

Pattern myPattern = Pattern.compile(myMatch);

Matcher myMatcher = myPattern.matcher(testString);

The replaceAll() method of the Matcher object is called with the replacement string Moon. The result is assigned to the testResult variable:

String testResult = myMatcher.replaceAll(“Moon”);

The original test string and a blank line as a separator are displayed:

System.out.println(“The test string is: \n’” + testString + “‘.”);

System.out.println();

The String class’s length() method is used to determine the length of testResult. If there is a match, the length of testResult will be greater than zero characters; therefore, the test for the if statement returns true, and the information about the replacement string is displayed:

if (testResult.length() > 0)

{

System.out.println(“After replacement the string is: \n’” + testResult + “‘.” );

}

643

Chapter 25

If the length of testResult is zero characters, a message is displayed indicating that no match was found:

else

{

System.out.println(“No match was found.”);

}

return true;

} // end myReplace()

The replaceFirst() Method

The replaceFirst() method replaces the first matching character sequence in the test string with a specified replacement string. The replacement string is the sole argument of the replaceFirst() method. The value returned is a String.

The reset() Method

The reset() method resets state information on a Matcher object. It can be used with no argument or with one argument. When the reset() method is used with no argument, the state information is reset. Any matching takes place from the beginning of the test string. The test string remains the same as previously. The value returned is a Matcher object.

When the reset() method is used with a single argument, the state information is reset. The reset() method’s argument is a String, which then becomes the test string for any future matching. The value returned is a Matcher object.

The start() Method

The start() method takes no argument. It returns an int value, which is the index of the first character in the most recent match.

The start() method can throw an IllegalStateException if matching has not started or if the most recent attempt at matching failed.

The PatternSyntaxException Class

A PatternSyntaxException object is an unchecked exception that indicates that there is an error in the syntax of a regular expression pattern.

The PatternSyntaxException class has methods that allow information about the exception to be accessed. The following table summarizes information about the methods of the PatternSyntax Exception class.

Method

Description

 

 

getDescription()

Retrieves the description of the error

getIndex()

Retrieves the error index in the pattern

getMessage()

Returns a multiline string that contains the description, pattern, and index

getPattern()

Retrieves the regular expression pattern that caused the error

644

Regular Expressions in Java

Metacharacters Suppor ted in the java.util.regex Package

The java.util.regex package supports an extensive range of metacharacters, which are briefly described in several tables in this section. Some of the metacharacters and character classes are described in more detail later in the section, with examples of how they can be used.

The following table summarizes many of the metacharacters supported in the Java java.util.regex package. The POSIX metacharacters supported in java.util.regex are listed in a later section in this chapter.

Metacharacter

Description

 

 

. (period character)

Matches any character. May or may not match line-terminator

 

characters.

\d

Matches a numeric digit. Equivalent to the character class [0-9].

\D

Matches a character that is not a numeric digit. Equivalent to the

 

character class [^0-9].

\s

Matches a whitespace character.

\S

Matches any character that is not a whitespace character.

\w

Matches a word character. Equivalent to the character class

 

[A-Za-z0-9_].

\W

Matches a character that is not a word character. Equivalent to the

 

negated character class [^A-Za-z0-9_].

[...]

Character class. Matches any single character contained between the

 

square brackets.

[a-d[w-z]]

The union of two character classes. Equivalent to [a-dw-z].

[a-m[h-z]]

The intersection of two character classes. Equivalent to [h-m].

[a-z[^h-m]]

The intersection of a character class and a negated character class.

 

The overall effect is subtraction of one character class from another.

 

Equivalent to [a-gn-z].

[^...]

Negated character class. Matches any single character, except those

 

characters contained between the square brackets.

 

 

Using the \d Metacharacter

The \d metacharacter matches a single numeric digit. The following example attempts to match U.S. Zip codes. A simple pattern \d{5}-\d{4}* can match abbreviated and extended U.S. Zip codes.

645

Chapter 25

Try It Out

sing the \d Metacharacter

1.Type the following code into a text editor:

import java.util.regex.*;

public class MatchZip{

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

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

//Attempt to match US Zip codes.

//The pattern matches five numeric digits followed by a hyphen followed by four numeric digits.

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

Pattern myPattern = Pattern.compile(myRegex);

Matcher myMatcher = myPattern.matcher(myTestString);

String myMatch = “”;

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

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

{

myMatch = myMatcher.group();

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

if (myMatch == “”){

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

}// end if

}// end main()

}

2.Save the code as MatchZip.java.

3.Compile the code by typing javac MatchZip.java at the command line.

4.Run the code by typing java MatchZip at the command line, and inspect the results, as shown in Figure 25-8.

Figure 25-8

646