353

I'm trying to replace uppercase letters with corresponding lowercase letters using regex. So that

EarTH:   1,
MerCury: 0.2408467,
venuS:   0.61519726,

becomes

earth:   1,
mercury: 0.2408467,
venus:   0.61519726,

in Sublime Text. How can I downcase letters only in words that contain both lower and uppercase letters? So that it affects venUs and not VENUS.

Braiam
  • 1
  • 11
  • 47
  • 78
leemour
  • 11,414
  • 7
  • 36
  • 43

8 Answers8

598

You may:

Find: (\w) Replace With: \L$1

Or select the text, ctrl+K+L.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • 17
    super. very useful. Could you please point to some resource that lists transformations like '\L' ? – Codious-JR Apr 03 '16 at 10:49
  • 26
    Sublime uses Boost for its RegEx support, these are the docs for the format strings like \L: http://www.boost.org/doc/libs/1_44_0/libs/regex/doc/html/boost_regex/format/perl_format.html – Alex K. Apr 03 '16 at 13:09
  • 9
    And in IntelliJ – avalancha May 09 '18 at 16:56
  • 32
    Note: To go to uppercase, you'll need `\U` – takanuva15 Jul 25 '18 at 20:01
  • 1
    Is it possible to only lowercase a single parameter in a replacement? For instance, `$1.\L$2($3)` to lowercase `$2` while not making `$3` lowercase? – NobleUplift Apr 23 '19 at 22:32
  • 13
    To only lowercase a single parameter, put \E after it to end the lowercase section: `$1\L$2\E$3` – Michael Sep 05 '19 at 18:53
  • 1
    @Patrick IntelliJ is the same syntax. Cmd/Ctrl-R for find/replace. Click the little `.*` icon to turn on regex. `$1` for the first matching group. `$2` for the second ... `$n` for the nth. You use the dollar syntax in the results. So the find would be `(\w)` the replace would be `\L$1`. – 8bitme Jun 06 '20 at 12:41
  • 1
    Just a note .NET Regex class doesn't honor replace tags like \L, \U, \E etc. – Jroonk Jul 19 '20 at 02:10
  • 5
    If anyone is here using VS Code, `\L$1` works there too! – zcoop98 Sep 25 '20 at 23:08
  • 1
    This doesn't work in Visual Studio's Find & Replace. – Matt Arnold Oct 06 '20 at 14:17
  • 3
    to lower a single letter you can also use lower case 'L' `\l$1` instead of `\L$1\E` – Takeya Nov 16 '21 at 10:05
206

I figured this might come in handy for others as well :

find:

  • ([A-Z])(.*)

replace:

  • \L$1$2 --> will convert all letters in $1 and $2 to lowercase
    BUT
  • \l$1$2 --> will only convert the first letter of $1 to lowercase and leave everything else as is

The same goes for uppercase with \U and \u

axblount
  • 2,639
  • 23
  • 27
dGo
  • 2,951
  • 1
  • 18
  • 11
  • 12
    `\l$1$2` would only make the whole `$1` in lower case in this case here, because `$i` contains only one letter. `\l` => first following letter to lower case and `\u` => first following letter to upper case. Where `\U` and `\I` doing it to all following letters. – Raisch Dec 30 '15 at 12:54
  • 18
    Note that `\E` terminates a `\L` or `\U` sequence. – Ludovic Kuty Feb 03 '19 at 10:31
112

Before searching with regex like [A-Z], you should press the case sensitive button (or Alt+C) (as leemour nicely suggested to be edited in the accepted answer). Just to be clear, I'm leaving a few other examples:

  1. Capitalize words
  • Find: (\s)([a-z]) (\s also matches new lines, i.e. "venuS" => "VenuS")
  • Replace: $1\u$2
  1. Uncapitalize words
  • Find: (\s)([A-Z])
  • Replace: $1\l$2
  1. Remove camel case (e.g. cAmelCAse => camelcAse => camelcase)
  • Find: ([a-z])([A-Z])
  • Replace: $1\l$2
  1. Lowercase letters within words (e.g. LowerCASe => Lowercase)
  • Find: (\w)([A-Z]+)
  • Replace: $1\L$2
  • Alternate Replace: \L$0
  1. Uppercase letters within words (e.g. upperCASe => uPPERCASE)
  • Find: (\w)([A-Z]+)
  • Replace: $1\U$2
  1. Uppercase previous (e.g. upperCase => UPPERCase)
  • Find: (\w+)([A-Z])
  • Replace: \U$1$2
  1. Lowercase previous (e.g. LOWERCase => lowerCase)
  • Find: (\w+)([A-Z])
  • Replace: \L$1$2
  1. Uppercase the rest (e.g. upperCase => upperCASE)
  • Find: ([A-Z])(\w+)
  • Replace: $1\U$2
  1. Lowercase the rest (e.g. lOWERCASE => lOwercase)
  • Find: ([A-Z])(\w+)
  • Replace: $1\L$2
  1. Shift-right-uppercase (e.g. Case => cAse => caSe => casE)
  • Find: ([a-z\s])([A-Z])(\w)
  • Replace: $1\l$2\u$3
  1. Shift-left-uppercase (e.g. CasE => CaSe => CAse => Case)
  • Find: (\w)([A-Z])([a-z\s])
  • Replace: \u$1\l$2$3

Regarding the question (match words with at least one uppercase and one lowercase letter and make them lowercase), leemour's comment-answer is the right answer. Just to clarify, if there is only one group to replace, you can just use ?: in the inner groups (i.e. non capture groups) or avoid creating them at all:

  • Find: ((?:[a-z][A-Z]+)|(?:[A-Z]+[a-z])) OR ([a-z][A-Z]+|[A-Z]+[a-z])
  • Replace: \L$1

2016-06-23 Edit

Tyler suggested by editing this answer an alternate find expression for #4:

  • (\B)([A-Z]+)

According to the documentation, \B will look for a character that is not at the word's boundary (i.e. not at the beginning and not at the end). You can use the Replace All button and it does the exact same thing as if you had (\w)([A-Z]+) as the find expression.

However, the downside of \B is that it does not allow single replacements, perhaps due to the find's "not boundary" restriction (please do edit this if you know the exact reason).

2023-08-24 Edit

To end changing the letter case, use \E

  • replace: $1\L$2\E$3 (e.g. (ABC)(ABC)(ABC) => ABCabcABC)
Radek Pech
  • 3,032
  • 1
  • 24
  • 29
Armfoot
  • 4,663
  • 5
  • 45
  • 60
4

Try this

  • Find: ([A-Z])([A-Z]+)\b
  • Replace: $1\L$2

Make sure case sensitivity is on (Alt + C)

legoscia
  • 39,593
  • 22
  • 116
  • 167
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
  • 1
    That doesn't work. It matches 2+ uppercase letters. But I understand that `\L` is for lowercase. I updated the question. – leemour Dec 23 '13 at 11:26
4

Regular expression

Find:\w+

Replace:\L$0

Sublime Text uses the Perl Compatible Regular Expressions (PCRE) engine from the Boost library to power regular expressions in search panels.

\L Converts everything up to lowercase

$0 Capture groups

BadTudou
  • 41
  • 3
4
  • FIND: (\u)
  • REPLACE: \L$1$2

Operation confirmed in Notepad++.
Do not forget to check the "Match case" box.

Max Ta
  • 41
  • 4
2

In BBEdit works this (ex.: changing the ID values to lowercase):

Search any value: <a id="(?P<x>.*?)"></a> Replace with the same in lowercase: <a id="\L\P<x>\E"></a>

Was: <a id="VALUE"></a> Became: <a id="value"></a>

2

To convert snake_case words with some additional fields starting with _:

EXAMPLE:

_id
user_name

to

id
userName

Use first:

  • FIND: (\w)_(\w)
  • REPLACE: $1\U$2

Then, just replace "_" to "" (Blank space)

JotaPardo
  • 817
  • 9
  • 27