Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

This tutorial presents Java powerful regular-expression processing capabilities using class Pattern, class Matcher and class String's matches method. This tutorial is intended for students and developers who are familiar with basic Java string-processing techniques.

Download the code for this tutorial here.

[Note: This tutorial is an excerpt (Section 29.7) of Chapter 29, Strings, Characters and Regular Expressions, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.1378-1387. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

29.7 Regular Expressions, Class Pattern and Class Matcher

Regular expressions are sequences of characters and symbols that define a set of strings. They are useful for validating input and ensuring that data is in a particular format. For example, a ZIP code must consist of five digits, and a last name must contain only letters, spaces, apostrophes and hyphens. One application of regular expressions is to facilitate the construction of a compiler. Often, a large and complex regular expression is used to validate the syntax of a program. If the program code does not match the regular expression, the compiler knows that there is a syntax error within the code.

Class String provides several methods for performing regular-expression operations, the simplest of which is the matching operation. String method matches receives a string that specifies the regular expression and matches the contents of the String object on which it is called to the regular expression. The method returns a boolean indicating whether the match succeeded. A regular expression consists of literal characters and special symbols. Figure 29.19 specifies some predefined character classes that can be used with regular expressions. A character class is an escape sequence that represents a group of characters. A digit is any numeric character. A word character is any letter (uppercase or lowercase), any digit or the underscore character. A whitespace character is a space, a tab, a carriage return, a newline or a form feed. Each character class matches a single character in the string we are attempting to match with the regular expression.

Character Matches Character Matches
\d any digit \D any non-digit
\w any word character \W any non-word character
\s any whitespace \S any non-whitespace
Fig. 29.19 Predefined character classes.

Regular expressions are not limited to these predefined character classes. The expressions employ various operators and other forms of notation to match complex patterns. We examine several of these techniques in the application in Fig. 29.20 and Fig. 29.21 which validates user input via regular expressions. [Note: This application is not designed to match all possible valid user input.]

   1  // Fig. 29.20: ValidateInput.java
2 // Validate user information using regular expressions.
3
4 public class ValidateInput
5 {
6 // validate first name
7 public static boolean validateFirstName( String firstName )
8 {
9 return firstName.matches( "[A-Z][a-zA-Z]*" );
10 } // end method validateFirstName
11
12 // validate last name
13 public static boolean validateLastName( String lastName )
14 {
15 return lastName.matches( "[a-zA-z]+([ '-][a-zA-Z]+)*" );
16 } // end method validateLastName
17
18 // validate address
19 public static boolean validateAddress( String address )
20 {
21 return address.matches(
22 "\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );
23 } // end method validateAddress
24
25 // validate city
26 public static boolean validateCity( String city )
27 {
28 return city.matches( "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );
29 } // end method validateCity
30
31 // validate state
32 public static boolean validateState( String state )
33 {
34 return state.matches( "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" ) ;
35 } // end method validateState
36
37 // validate zip
38 public static boolean validateZip( String zip )
39 {
40 return zip.matches( "\\d{5}" );
41 } // end method validateZip
42
43 // validate phone
44 public static boolean validatePhone( String phone )
45 {
46 return phone.matches( "[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}" );
47 } // end method validatePhone
48 } // end class ValidateInput
 Fig. 29.20  Validating user information using regular expressions.
Page 1 | 2 | 3 | 4 | 5
Return to Tutorial Index