Use Java and RegEx to convert casing in a string

You can't do this in Java regex. You'd have to manually post-process using String.toUpperCase() and toLowerCase() instead.

Here's an example of how you use regex to find and capitalize words of length at least 3 in a sentence

    String text = "no way oh my god it cannot be";
    Matcher m = Pattern.compile("\\b\\w{3,}\\b").matcher(text);

    StringBuilder sb = new StringBuilder();
    int last = 0;
    while (m.find()) {
        sb.append(text.substring(last, m.start()));
        sb.append(m.group(0).toUpperCase());
        last = m.end();
    }
    sb.append(text.substring(last));

    System.out.println(sb.toString());
    // prints "no WAY oh my GOD it CANNOT be"

Note on appendReplacement and appendTail

Note that the above solution uses substring and manages a tail index, etc. In fact, you can go without these if you use Matcher.appendReplacement and appendTail.

    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, m.group().toUpperCase());
    }
    m.appendTail(sb);

Note how sb is now a StringBuffer instead of StringBuilder. Until Matcher provides StringBuilder overloads, you're stuck with the slower StringBuffer if you want to use these methods.

It's up to you whether the trade-off in less efficiency for higher readability is worth it or not.

See also

  • StringBuilder and StringBuffer in Java

Java9+

From Java 9+ you can use Matcher::replaceAll where you can use a Function<MatchResult, String> for example we use the example of polygenelubricants :

String text = "this is just a test which upper all short words";
String regex = "\\b\\w{0,3}\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
String result = matcher.replaceAll(matche -> matche.group().toUpperCase());

System.out.println(result);

Or Just :

String result = Pattern.compile(regex)
        .matcher(text)
        .replaceAll(matche -> matche.group().toUpperCase());

Output

this IS just A test which upper ALL short words
     ^^      ^                  ^^^

To do this on regexp level you have to use \U to switch on uppercase mode and \E to switch it off. Here is an example how to use this feature in IntelliJ IDEA find-and-replace dialog which transforms set of class fields to JUnit assertions (at IDE tooltip is a result of find-and-replace transformation):

enter image description here


You could use the regexp capturing group (if you really need to use regex, that is, meaning if "TARGETSTRING" is complex enough and "regular" enough to justify being detected by a regex).
You would then apply toLowerCase() to the group #1.

import java.util.regex.*;

public class TargetToLowerCase {

  public static void main(String[] args) {
    StringBuilder sb= new StringBuilder(
            "my testtext TARGETSTRING my testtext");
    System.out.println(sb);
    String regex= "TARGETSTRING ";
    Pattern p = Pattern.compile(regex); // Create the pattern.
    Matcher matcher = p.matcher(sb); // Create the matcher.
    while (matcher.find()) {
      String buf= sb.substring(matcher.start(), matcher.end()).toLowerCase();
      sb.replace(matcher.start(), matcher.end(), buf);
    }
    System.out.println(sb);
  }
}