java
26 lines · 7 steps
Converting camelCase to snake_case in Java
A regex that spots case boundaries turns camelCase identifiers into snake_case with underscores.
Explained by
highlit
1public final class CaseConverter {
2
3 private static final Pattern CAMEL_BOUNDARY =
4 Pattern.compile("([a-z0-9])([A-Z])|([A-Z]+)([A-Z][a-z])");
5
6 private CaseConverter() {
7 }
8
9 public static String toSnakeCase(String camelCase) {
10 if (camelCase == null || camelCase.isEmpty()) {
11 return camelCase;
12 }
13
14 Matcher matcher = CAMEL_BOUNDARY.matcher(camelCase);
15 StringBuilder result = new StringBuilder();
16 while (matcher.find()) {
17 String replacement = matcher.group(1) != null
18 ? matcher.group(1) + "_" + matcher.group(2)
19 : matcher.group(3) + "_" + matcher.group(4);
20 matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
21 }
22 matcher.appendTail(result);
23
24 return result.toString().toLowerCase(Locale.ROOT);
25 }
26}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Compiling a Pattern once as a static constant avoids re-parsing the regex on every call.
- 2Two alternations let a single regex handle both normal boundaries and acronym-to-word transitions.
- 3appendReplacement plus appendTail rebuild a string while touching only the matched regions.
Related explainers
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/converting-camelcase-to-snake_case-in-java-explained-java-3220/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.