JDK versions 1.4.0 and later have comprehensive support for regular expressions through the standard java.util.regex package. Because Java lacked a regex package for so long, there are also many 3rd party regex packages available for Java. I will only discuss Sun's regex library that is now part of the JDK. Its quality is excellent, better than most of the 3rd party packages. Unless you need to support older versions of the JDK, the java.util.regex package is the way to go.
Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set (ie. by pattern matching). They can be used as a tool to search, edit or manipulate text or data. One common use is validation of data entry strings.
All classes related to regular expressions are found in the java.util.regex package which must be imported.
Java regular expression patterns use a syntax similar to the one used by perl. The best reference is found at sun.com.
Simple examples of the use of regular expressions are:
Code:
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches(); As a convenience for a one-time use situation the matches method simplifies the syntax (but does not precompile the pattern).
Code:
boolean b = Pattern.matches("a*b", "aaaaab");