Replacement method for single pattern and single alternate text
public static int substitute (StringBuffer result,
Patternmatcher Matcher, pattern pattern,
Substitution Sub, patternmatcherinput input,
int numsubs)
{
int Beginoffset, Subcount;
Char[] InputBuffer;
Subcount = 0;
Beginoffset = Input.getbeginoffset ();
The following methods, relative to the copy of input and saved as InputBuffer
InputBuffer = Input.getbuffer ();
Must be!= 0 because Substitute_all is represented by-1.
Don't change to numsubs > 0.
while (numsubs!= 0 && matcher.contains (input, pattern)) {
--numsubs;
++subcount;
Copy the contents of the original text that do not need to be replaced directly into result
Result.append (InputBuffer, Beginoffset,
Input.getmatchbeginoffset ()-beginoffset);
Replace the match in the original and save the content to result
Sub.appendsubstitution (result, Matcher.getmatch (), Subcount,
Input, matcher, pattern);
Update the starting position of the next loop, the location that has already been matched is no longer retrieved
Beginoffset = Input.getmatchendoffset ();
}
Copy the last unwanted match in the original to result
result.append (InputBuffer, Beginoffset, Input.length ()-beginoffset);
return subcount;
}
}