Regular Expressions

A regular expression (or regex for short) is a special type of pattern-matching string that can be very useful for programs that do string manipulation. Regular expression strings contain special pattern-matching characters that can be matched against another string to see whether the other string fits the pattern. Regular expressions are very handy for doing complex data validation, such as making sure that users enter properly formatted phone numbers, e-mail addresses, or Social Security numbers, for example.

Regular expressions are also useful for many other purposes, including searching text files to see whether they contain certain patterns, filtering e-mail based on its contents, or performing complicated search-and-replace functions.

This section presents important reference information for forming regular expressions. For information about using regular expressions in a Java program, see Pattern Class and Matcher Class.

Remember.eps Many regex patterns use the backslash as an escape symbol. Unfortunately, the backslash is also an escape symbol in Java. Thus, to create a Java string that has a regex pattern that contains a backslash, you must code two consecutive backslashes in your Java program. For example, suppose you want to create a string variable named pattern and assign it the regex pattern \w. To do that, you would write code similar to the following:

string ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.