VS Code Regex find and replace with lowercase, use \l or \L if possible
Asked Answered
E

2

91

Is there a way to do a find and replace (regex) all uppercase characters in the matching strings with lowercase ones? Preferably in VS Code or IntelliJ I already have my regex ready.

Edit: To be clear I already know who to find the matches. But looking for that function to replace all uppercase matches with lowercase ones

Exposition answered 17/1, 2020 at 10:4 Comment(0)
C
80
  • Press Ctrl + F
  • Select .* button and enter your RegEx
  • Press Ctrl + Shift + L (Windows) or Cmd + Shift + L (Mac) to select all matched results
  • Press Ctrl + Shift + P (Windows) or Cmd + Shift + P (Mac)
  • Choose Transform to Lowercase

If you want to modify only part of the matching text you have to do 1 step extra.

If you press Ctrl + Shift + L in the Find dialog it selects the full matching text but you can't move the (multi) cursors and make a partial selection.

After entering the regex, VSC will show you which parts will match the find.

  • Click somewhere in the file
  • Press Ctrl + Shift + L (Select All)

or

  • Press Alt + Enter (in the Find dialog)

Now you can move the (multi) cursors and make a partial selection and apply the needed transform.

Cachepot answered 17/1, 2020 at 18:38 Comment(3)
@Baryon I thought the focus had to be on the editor, but Shift+Ctrl+L also works from the search box. Always learn a new thing a day.Cachepot
When I tried to use Ctrl + Shift + L I got this message: The number of cursors has been limited to 10000.Dirge
@Dirge That is the limit that is hard coded in VSC, max 10000 multi cursors, then you have to do it in a few parts or write a Python/Node script that processes the file(s)Cachepot
B
133

There is support for the case modifiers \L, \l, \U and \u Find/Replace (from Build 1.47 for replacing strings in an editor, and from Build 1.49 it also works in the Find/Replace across the workspace (see https://github.com/microsoft/vscode/pull/105101)).

So you just have to put the \l modifier in front of all your matched uppercase groups, like

\l$1 see regex101 demo1

or just put the \L in front of it all, like \L(rest of replace here). see regex1010 demo2


Note that these modifiers work a little differently than you might be used to. For instance:

(de)(pth) Search

\U$1$2 Replace

DEPTH expected result

DEpth vscode result

The case modifier only works on the immediate capture group. Not until it encounters \E or the end of the replace string.

I assume for this same reason \E isn't implemented in vscode at all, as it would be irrelevant given that only the immediate capture group is modified.

So to get DEPTH as the result you should use \U$1\U$2.

The modifiers can also be stacked - for example, \u\u\u$1 will uppercase the first 3 characters of the group, or \l\U$1 will lowercase the first character, and uppercase the rest.

Currently, these are only supported in the editor's find widget, and not in "Find in Files".

Baryon answered 8/6, 2020 at 20:1 Comment(4)
As noted, delivered in 1.47Overweigh
Worth noting this is currently only available in "Find and replace" and not "Find in files"Bastogne
@Bastogne It does work in a search across files. What are you trying to do precisely?Baryon
@Baryon thanks, realised it only works with $1+ and not $0 like in some other regex toolsBastogne
C
80
  • Press Ctrl + F
  • Select .* button and enter your RegEx
  • Press Ctrl + Shift + L (Windows) or Cmd + Shift + L (Mac) to select all matched results
  • Press Ctrl + Shift + P (Windows) or Cmd + Shift + P (Mac)
  • Choose Transform to Lowercase

If you want to modify only part of the matching text you have to do 1 step extra.

If you press Ctrl + Shift + L in the Find dialog it selects the full matching text but you can't move the (multi) cursors and make a partial selection.

After entering the regex, VSC will show you which parts will match the find.

  • Click somewhere in the file
  • Press Ctrl + Shift + L (Select All)

or

  • Press Alt + Enter (in the Find dialog)

Now you can move the (multi) cursors and make a partial selection and apply the needed transform.

Cachepot answered 17/1, 2020 at 18:38 Comment(3)
@Baryon I thought the focus had to be on the editor, but Shift+Ctrl+L also works from the search box. Always learn a new thing a day.Cachepot
When I tried to use Ctrl + Shift + L I got this message: The number of cursors has been limited to 10000.Dirge
@Dirge That is the limit that is hard coded in VSC, max 10000 multi cursors, then you have to do it in a few parts or write a Python/Node script that processes the file(s)Cachepot

© 2022 - 2024 — McMap. All rights reserved.