Sample code for 30+ languages & platforms
Java Requires Chilkat v11.1.0+

Regular Expression Replace Full Matches

See more Regular Expressions Examples

Demonstrates replacing the full matches of a regular expression.

Note: Chilkat uses PCRE2. See PCRE2 Regular Expressions
Also see: PCRE2 Performance

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        String subject = "John Anders, +_+_+ Mary Robins $$$$";
        String pattern = "\\w+\\s+\\w+";

        StringBuilder sb = new StringBuilder();
        sb.append(subject);

        JsonObject json = new JsonObject();
        json.setEmitCompact(false);

        int timeoutMs = 2000;
        int numMatches = sb.regexMatch(pattern, json, timeoutMs);
        if (numMatches < 0) {
            // Probably an error in the regular expression.
            // Suggestion: Use AI to help create and/or diagnose regular expressions.
            System.out.println(sb.getLastErrorText());
            return;
        }

        // Examine the matches:
        System.out.println(json.emit());

        // {
        //   "match": [
        //     {
        //       "group": [
        //         {
        //           "cap": "John Anders",
        //           "idx": 0,
        //           "len": 11
        //         }
        //       ]
        //     },
        //     {
        //       "group": [
        //         {
        //           "cap": "Mary Robins",
        //           "idx": 19,
        //           "len": 11
        //         }
        //       ]
        //     }
        //   ]
        // }

        StringBuilder sbTemp = new StringBuilder();

        int i = 0;
        numMatches = json.sizeOfArray("match");
        while (i < numMatches) {
            json.setI(i);

            // The full match is always in group 0.
            sbTemp.clear();
            json.stringOfSb("match[i].group[0].cap", sbTemp);

            // Indicate that we wish to replace the full match with it's value converted to uppercase.
            sbTemp.toUppercase();
            json.updateSb("match[i].group[0].rep", sbTemp);

            i = i + 1;
        }

        // The JSON now has replacement strings:
        System.out.println(json.emit());

        // {
        //   "match": [
        //     {
        //       "group": [
        //         {
        //           "cap": "John Anders",
        //           "idx": 0,
        //           "len": 11,
        //           "rep": "JOHN ANDERS"
        //         }
        //       ]
        //     },
        //     {
        //       "group": [
        //         {
        //           "cap": "Mary Robins",
        //           "idx": 19,
        //           "len": 11,
        //           "rep": "MARY ROBINS"
        //         }
        //       ]
        //     }
        //   ]
        // }

        // Call RegexReplace to update the StringBuilder with the replacements.
        try {
            sb.regexReplace(json);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Result after doing replacements:");
        System.out.println(sb.getAsString());

        // Result after doing replacements:
        // JOHN ANDERS, +_+_+ MARY ROBINS $$$$
    }
}