Regex Cheatsheet for Text Case Conversion

A regular expression can find text in any case, but changing the case of what it found is the job of the replacement string, and support varies a lot. GNU sed, Perl, Vim, PCRE2, Notepad++ and JetBrains IDEs understand the case escapes \U, \L, \u, \l and \E. VS Code understands the first four but applies each one only to the next capture group. JavaScript and Python have none of them, so you pass a function instead.
This cheatsheet covers the syntax, which tool does what, the ordering trap that catches people out, and tested recipes for the conversions you'll actually need.
The case escapes at a glance
| Escape | What it does |
|---|---|
\U | Uppercase everything that follows, until \E or \L |
\L | Lowercase everything that follows, until \E or \U |
\u | Uppercase the next character only |
\l | Lowercase the next character only |
\E | Stop the conversion started by \U or \L |
They go in the replacement, next to your group references: \U\1 in sed and Vim, \U$1 in Perl, VS Code and JetBrains IDEs. Inside the pattern they mean something else entirely (in PCRE2, \U in a pattern is an error by default).
Which tools support \U and \L in the replacement
| Tool | Case escapes | Notes |
|---|---|---|
| GNU sed | \U \L \u \l \E | A GNU extension. The BSD sed that ships with macOS doesn't have them. |
| Perl | \U \L \u \l \E | They also work in ordinary double-quoted strings. |
Vim (:s) | \U \L \u \l \E | \e also ends a conversion. |
| PCRE2 | \U \L \u \l \E | Only when pcre2_substitute() runs with PCRE2_SUBSTITUTE_EXTENDED. |
| Notepad++ | \U \L \u \l \E | In Regular expression search mode. |
| JetBrains IDEs | \U \L \u \l \E | Documented for IntelliJ IDEA's Find and Replace. |
| VS Code | \U \L \u \l | Since version 1.47. No \E; each escape applies to the next group only. |
JavaScript replace() | None | Use a replacement function. |
Python re.sub() | None | \U in the template raises re.error: bad escape. Use a function. |
Our own Find & Replace tool runs JavaScript's replace() under the hood, so $1 works there but \U comes out as literal text.
VS Code applies case escapes to one group at a time
VS Code added case changes to regex replace in version 1.47, and they now work in both the editor's Find control and Find in Files. The catch is scope. In sed, Perl and Vim, \U switches on uppercase until something switches it off. In VS Code, each escape attaches itself to the next $1, $2 or $& and affects only that group:
| Replacement on "hello world" (Perl and VS Code syntax) | sed, Perl, Vim | VS Code |
|---|---|---|
\U$1_$2 | HELLO_WORLD | HELLO_world |
\U$1\E $2 | HELLO world | HELLO\E world |
Literal text is never converted either: \Uabc$1 gives abcHELLO. If you want two groups uppercased in VS Code, write \U$1_\U$2. There's more on the editor's own case commands in our VS Code case conversion guide.
Title-casing a group: \u\L or \L\u?
To turn hELLO into Hello you combine two escapes: uppercase the first letter, lowercase the rest. The order matters more than you'd expect, and the tools disagree:
- Perl and Vim accept either order. Both
\u\L$1and\L\u$1giveHello. - PCRE2 documents
\u\L$1as its title-casing idiom. - VS Code needs
\u\L$1. Written the other way round, you gethello. - GNU sed is the reverse:
\L\u\1works, and\u\L\1giveshello(tested on sed 4.9).
If a recipe that worked in one tool silently lowercases everything in another, this is the first thing to check.
Tested recipes
snake_case to camelCase
sed -E 's/_([a-z0-9])/\U\1/g' # user_id_2 first_name -> userId2 firstName
perl -pe 's/_([a-z0-9])/\U$1/g'
VS Code: Find _([a-z0-9]) Replace \U$1camelCase to snake_case
sed -E 's/([a-z0-9])([A-Z])/\1_\L\2/g' # userId firstName -> user_id first_name
VS Code: Find ([a-z0-9])([A-Z]) Replace $1_\l$2This is fine for ordinary names and wrong for acronyms: XMLHttpRequest becomes XMLHttp_request. Acronyms need a second rule that splits where a run of capitals meets a capitalized word, and at that point a short script is easier to read than a one-liner.
Capitalize every word without breaking "don't"
sed -E 's/\b([a-z])/\u\1/g' # don't stop o'neil -> Don'T Stop O'Neil
sed -E 's/(^|\s)([a-z])/\1\u\2/g' # don't stop o'neil -> Don't Stop O'neil
VS Code: Find (^|\s)([a-z]) Replace $1\u$2The \b version sees a word boundary after every apostrophe. Anchoring to the start of the line or a space fixes contractions, at the cost of names like O'Neil. No regex knows which apostrophes belong to names.
Calm down SHOUTING but keep short acronyms
sed -E 's/\b([A-Z])([A-Z]{3,})\b/\1\L\2/g'
# HELLO WORLD, it is OK. NASA SAYS HI -> Hello World, it is OK. Nasa Says HIRequiring four or more capitals spares OK and HI but not NASA. Pick the threshold for your text, or keep a list of acronyms to restore afterwards.
Uppercase SQL keywords
sed -E 's/\b(select|from|where|order by)\b/\U\1/gI'
# select id from users where id = 1 order by id -> SELECT id FROM users WHERE id = 1 ORDER BY idThe I flag makes the match case-insensitive, so mixed-case keywords are caught too. In VS Code, leave Match Case off and replace with \U$1.
camelCase to CONSTANT_CASE
perl -pe 's/([a-z0-9])([A-Z])/$1_$2/g; $_ = uc' # userId apiKey -> USER_ID API_KEYJavaScript and Python: pass a function instead
Neither language has case escapes, but both let the replacement be a function that receives the match and its groups and returns whatever you like. In JavaScript the function gets the whole match, then each group, then the offset and the input string (see MDN's reference for replace()):
"user_id first_name".replace(/_([a-z0-9])/g, (match, letter) => letter.toUpperCase());
// "userId firstName"
// \b and \w only know ASCII letters in JavaScript:
"élan über".replace(/\b\w/g, (c) => c.toUpperCase());
// "éLan üBer"
// Unicode-aware version: \p{L} with the u flag
"élan über".replace(/(^|\s)(\p{L})/gu, (m, gap, c) => gap + c.toUpperCase());
// "Élan Über"Python works the same way with re.sub. Its \w is Unicode-aware for strings, which avoids the accent problem, but the apostrophe problem is still there, including in the built-in str.title():
import re, string
re.sub(r"_([a-z0-9])", lambda m: m.group(1).upper(), "user_id first_name")
# 'userId firstName'
re.sub(r"_([a-z0-9])", r"\U\1", "user_id")
# raises re.error: bad escape \U at position 0
"they're bill's friends from the UK".title()
# "They'Re Bill'S Friends From The Uk"
string.capwords("they're bill's friends from the UK")
# "They're Bill's Friends From The Uk" (splits on whitespace only)Locale and Unicode gotchas in sed
In sed, [a-z] doesn't match accented letters, so élan is skipped by every recipe above. [[:lower:]] does match accented letters, but only when sed runs under a UTF-8 locale; under the plain C locale it doesn't. If a recipe catches accented letters on your laptop and misses them on a server or in a container, run locale before you start doubting the regex.
When a regex is the wrong tool
Regex replacements are great for structural changes: separators, prefixes, one group at a time. They're poor at anything that needs judgement. Real title case keeps "of" and "the" lowercase, sentence case needs to know where sentences end, and both are easier with our case converter than with a pattern. For the cases where a regex is right, build and check the pattern first in our regex tester, which uses JavaScript's engine and flags, then move it to the tool that does the replacement.
A rule of thumb before you start: look up the replacement syntax, not the pattern syntax. If the tool's documentation doesn't mention \U, it doesn't have it, and you'll need a function or a different tool.


