Regex powerful tool: Org.apache.oro.text.regex under the Util source code interpretation of the Substitute () Method 1

Source: Internet
Author: User

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;
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.