Powerful RegEx tool: util source code interpretation under org. Apache. Oro. Text. RegEx-substitute () method 1

Source: Internet
Author: User

 

// Method for replacing a single pattern and a single replacement text

Public static int substitute (stringbuffer result,
Patternmatcher, pattern,
Substitution sub, patternmatcherinput input,
Int numsubs)
{
Int beginoffset, subcount;
Char [] inputbuffer;

Subcount = 0;
Beginoffset = input. getbeginoffset ();

// The following method copies the input and saves it as inputbuffer.
Inputbuffer = input. getbuffer ();

// Must be! = 0 because substitute_all is represented by-1.
// Do not change to numsubs> 0.
While (numsubs! = 0 & matcher. Contains (input, pattern )){
-- Numsubs;
++ Subcount;

// Copy the content in the original text that does not need to be replaced to the result directly.
Result. append (inputbuffer, beginoffset,
Input. getmatchbeginoffset ()-beginoffset );

// Replace the content matched in the original text and save the content to the result.
Sub. appendsubstitution (result, matcher. getmatch (), subcount,
Input, matcher, pattern );

// Update the start position of the next loop. The matched position is no longer retrieved.
Beginoffset = input. getmatchendoffset ();
}

// Copy the last unmatched content in the original text to the 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.