Use Java and Regex to Convert Casing in a String

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

Java - Converting character of string to uppercase using RegEx

  1. public String replaceAll​(String replacement) doesn't modify source text used by Matcher, but it returns new (separate) String with replaced content based on original text passed to matcher. So p.matcher(str).replaceAll("\\p{Lu}$1"); will not modify str but will return new String which you are ignoring.

  2. Also .replaceAll("\\p{Lu}$1") doesn't treat \\p{Lu} as indicator of uppercase change like \U does in JavaScript. Java's regular expression engine is different beast and \\p{Lu} in replacement is treated as simple string without special meaning, so your code would result in thisp{Lu}-isp{Lu}-ap{Lu}_test.

If you want to easily generate dynamic replacement based on current match you can use public String replaceAll​(Function<MatchResult,​String> replacer) added in Java 9. This way you can provide implementation of Function<MatchResult,​String> functional interface with lambda like match -> /*code using match and returning proper replacement*/

In your case it can look like:

StringBuilder str = new StringBuilder("this-is-a_test");
Pattern p = Pattern.compile("[-_][a-z]");
String replaced = p.matcher(str).replaceAll(match -> match.group(0).toUpperCase());
System.out.println(replaced);

which results in this-Is-A_Test.

BTW I simplified your regex to [-_][a-z] since IMO it is easier to understand.

What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?

Another option is using Google Guava's com.google.common.base.CaseFormat

George Hawkins left a comment with this example of usage:

CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "THIS_IS_AN_EXAMPLE_STRING");

How to use Regex to convert initial letter of a string to upper case

You may match any lowercase letter that is preceded with a ":

Search:   (?<=")\p{Ll}

Replace\U$0

See the regex demo. Check Match Case to ensure matching only lower case letters.

Details

  • (?<=") - a positive lookbehind that ensure that the preceding char is a "
  • \p{Ll} - any Unicode lowercase letter.

Note that \U - uppercase conversion operator - does not require the trailing \E if you needn't restrict its scope and $0 backreference refers to the whole match value, no need to wrap the whole pattern with a capturing group.

Replace regex pattern to lowercase in java

It looks like you want to change any uppercase character which is not inside ${...} to its lowercase form.

With construct

Matcher matcher = ...

StringBuffer buffer = new StringBuffer();
while (matcher.find()){
String matchedPart = ...
...
matcher.appendReplacement(buffer, replacement);
}
matcher.appendTail(buffer);
String result = buffer.toString();

or since Java 9 we can use Matcher#replaceAll​(Function<MatchResult,String> replacer) and rewrite it like

String replaced = matcher.replaceAll(m -> {
String matchedPart = m.group();
...
return replacement;
});

you can dynamically build replacement based on matchedPart.

So you can let your regex first try to match ${...} and later (when ${..} will not be matched because regex cursor will not be placed before it) let it match [A-Z]. While iterating over matches you can decide based on match result (like its length or if it starts with $) if you want to use use as replacement its lowercase form or original form.

BTW regex engine allows us to place in replacement part $x (where x is group id) or ${name} (where name is named group) so we could reuse those parts of match. But if we want to place ${..} as literal in replacement we need to escape \$. To not do it manually we can use Matcher.quoteReplacement.

Demo:

String yourUrlString = "http://BLABLABLA?qUERY=sth¯o1=${MACRO_STR1}¯o2=${macro_str2}";

Pattern p = Pattern.compile("\\$\\{[^}]+\\}|[A-Z]");
Matcher m = p.matcher(yourUrlString);

StringBuffer sb = new StringBuffer();
while(m.find()){
String match = m.group();
if (match.length() == 1){
m.appendReplacement(sb, match.toLowerCase());
} else {
m.appendReplacement(sb, Matcher.quoteReplacement(match));
}
}
m.appendTail(sb);
String replaced = sb.toString();
System.out.println(replaced);

or in Java 9

String replaced = Pattern.compile("\\$\\{[^}]+\\}|[A-Z]")
.matcher(yourUrlString)
.replaceAll(m -> {
String match = m.group();
if (match.length() == 1)
return match.toLowerCase();
else
return Matcher.quoteReplacement(match);
});
System.out.println(replaced);

Output: http://blablabla?query=sth¯o1=${MACRO_STR1}¯o2=${macro_str2}

Java Regex To Uppercase

If you just have to convert cc to uppercase, and if it is fixed, then you can just replace the match with CC.

There is no one-liner generic solution for this in Java. You have to do this with Matcher#appendReplacement() and Matcher#appendTail():

String str = "Refurbished Engine for 2000cc Vehicles";
Pattern pattern = Pattern.compile("\\d{4}cc");
Matcher matcher = pattern.matcher(str);

StringBuffer result = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(result, matcher.group().toUpperCase());
}

matcher.appendTail(result);

System.out.println(result.toString());

Regex for converting CamelCase to camel_case in java

See this question and CaseFormat from guava

in your case, something like:

CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "SomeInput");

Regex replace the substitution string

Since Java 9, we can provide a Function to Matcher#replaceAll(Function<MatchResult,​String> replacer). It is more concise than other answers here. Eg:

Pattern.compile("regexp")
.matcher(str)
.replaceAll(mr -> mr.group().toUpperCase());

We can fully customize this behaviour since we have a hold on MatchResult:

Pattern.compile("regexp")
.matcher(str)
.replaceAll(mr -> {
String.format("%s %s",
mr.group(1).toUpperCase),
mr.group(2).indent(4);
});


Related Topics



Leave a reply



Submit