public static void main (String [] argv) {
package java.lang; public final class String implements java.io.Serializable, Comparable, CharSequence { // This is a partial API listing public boolean matches (String regex) public String [] split (String regex) public String [] split (String regex, int limit) public String replaceFirst (String regex, String replacement) public String replaceAll (String regex, String replacement) } All the new String methods are pass-through calls to methods of the Pattern or Matcher classes. Now that you know how Pattern and Matcher are used and inter-operate, using these String convenience methods should be a no brainer. Instead of describing each method, they are summarized in Table 5-6. Table 5-6. Regular expression methods of the String class String method signature java.util.regex equivalent input.matches (String regex) Pattern.matches (String regex, CharSequence input) input.split (String regex) pat.split (CharSequence input) input.split (String regex, int limit) pat.split (CharSequence input, int limit) input.replaceFirst (String regex, String replacement) match.replaceFirst (String replacement) input.replaceAll (String regex, String replacement) match.replaceAll (String replacement) In Table 5-6, assume that there is a String named input, a Pattern object named pat, and a Matcher named match: String input = “Mary had a little lamb”; String [] tokens = input.split (”\s+”); // split on whitespace As of JDK 1.4, none of these regular expression convenience methods cache any expressions or do any other optimizations. Some JVM implementations may choose to cache and reuse pattern objects, but you should not rely on them. If you expect to apply the same pattern-matching operations repeatedly, it will be more efficient to use the classes in java.util.regex. 5.4 Java Regular Expression Syntax Following is a summary of the regular expression syntax supported by the java.util.regex package, as released in JDK 1.4. Things change quickly in the Java world, so you should always check the current documentation provided with the Java implementation you’re using. The information provided here is a quick reference to get you started. 186
Hint: This post is supported by Gama web hosting hrvatska services