, " " 5.2.3 The Matcher Class The
public Matcher appendReplacement (StringBuffer sb, String replacement) } Instances of the Matcher class are stateful objects that encapsulate the matching of a specific regular expression against a specific input character sequence. Matcher objects are not thread-safe because they hold internal state between method invocations. Every Matcher instance is derived from a Pattern instance, and the pattern() method of Matcher returns a back reference to the Pattern object that created it. Matcher objects can be used repeatedly, but because of their stateful nature, they must be placed in a known state to begin a new series of matching operations. This is done by calling the reset() method, which prepares the object for pattern matching at the beginning of the CharSequence associated with the matcher. The no-argument version of reset() will reuse the last CharSequence given to the Matcher. If you want to perform matching against a new sequence of characters, pass a new CharSequence to reset(), and subsequent matching will be done against that target. For example, as you read each line of a file, you could pass it to reset(). See Example 5-4. Example 5-4. Simple file grep package com.ronsoft.books.nio.regex; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; /** * Simple implementation of the ubiquitous grep command. * First argument is the regular expression to search for (remember to * quote and/or escape as appropriate). All following arguments are * filenames to read and search for the regular expression. * * @author Ron Hitchens (ron@ronsoft.com) */ public class SimpleGrep{ public static void main (String [] argv) throws Exception { if (argv.length < 2) { System.out.println ("Usage: regex file [ ... ]"); return; } Pattern pattern = Pattern.compile (argv [0]); Matcher matcher = pattern.matcher (""); for (int i = 1; i < argv.length; i++) { String file = argv [i]; 174
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services