To generate a replacement sequence of ab, the

The two append methods listed in the Matcher API are useful when iterating though an input character sequence, repeatedly invoking find(): package java.util.regex; public final class Matcher{ // This is a partial API listing public StringBuffer appendTail (StringBuffer sb) public Matcher appendReplacement (StringBuffer sb, String replacement) } Rather than returning a new String with the replacement already performed, the append methods append to a StringBuffer object you provide. This allows you to make decisions about the replacement at each point a match is found or to accumulate the result of matching against multiple input strings. Using appendReplacement() and appendTail() gives you total control of the search-and-replace process. One of the bits of state information remembered by Matcher objects is an append position. The append position is used to remember the amount of the input character sequence that has already been copied out by previous invocations of appendReplacement(). When appendReplacement() is invoked, the following process takes place: 1. Characters are read from the input starting at the current append position and appended to the provided StringBuffer. The last character copied is the one just before the first character of the matched pattern. This is the character at the index returned by start() minus one. 2. The replacement string is appended to the StringBuffer and substitutes any embedded capture group references as described earlier. 3. The append position is updated to be the index of the character following the matched pattern, which is the value returned by end(). The appendReplacement() method works properly only if a previous match operation was successful (usually a call to find()). You will be rewarded with a delightful java.lang.IllegalStateException if the last match returned false or if the method is called immediately following a reset. But don’t forget that there may be remaining characters in the input beyond the last match of the pattern. You probably don’t want to lose those, but appendReplace-ment() will not have copied them otherwise, and end() won’t return a useful value after find() fails to find any more matches. The appendTail() method is there to copy the remainder of your input in this situation. It simply copies any characters from the current append position to the end of the input and appends them to the given StringBuffer. The following code is a typical usage scenario for appendReplacement() and appendTail(): Pattern pattern = Pattern.compile (”([Tt])hanks”); 183
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services

Comments are closed.