Daniel Kaplan Daniel Kaplan - 7 months ago 126
Java Question

How can I use a regex to replace upper case with lower case in Intellij IDEA?

I've googled for this and found out how to do with with other regex parsers:

http://vim.wikia.com/wiki/Changing_case_with_regular_expressions
http://www.regular-expressions.info/replacecase.html


I've tried these and neither work. As an example, I want to use a regex to change this:

private String Name;
private Integer Bar = 2;


To this:

private String name;
private Integer bar = 2;


I tried something like this:

replace: private (\S+) (\S+)
with: private $1 $L$2
with: private $1 \L$2
with: <etc.>


None of them work. Is it possible to do this in intellij, or is this a missing feature? This is just for educational purposes and the example is contrived. I just want to know if this is possible to do in intellij.

Answer

In IDEA 15 you're able to use the below switches to toggle the case of captured expressions. This is now officially documented since this version was released.

  • \l: lower the case of the one next character
  • \u: up the case of the one next character
  • \L: lower the case of the next characters until a \E or the end of the replacement string
  • \U: up the case of the next characters until a \E or the end of the replacement string
  • \E: mark the end of a case change initiated by \U or \L

Here is an example usage (as the documentation is not clear):

find: (\w+_)+(\w+) replace: \L$1$2\E

The above will convert FOO_BAR_BAZ to foo_bar_baz etc The $1 refers to the first found capture group (in parenthesis), $2 to the second set, etc.

For posterity's sake: this was initially reported by @gaoagong and documented there.

Comments