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

JavaRegexIntellij Idea

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

Searched for the answer and then realized that @ajp15243 has already answered this above. There is currently no way in Intellij using their regex replacement feature to change the case of a letter. There is a short discussion at the following URL about the feature.

http://www.jetbrains.com/idea/webhelp/regular-expression-syntax-reference.html

You can also vote for the feature in the Youtrack issue here:

http://youtrack.jetbrains.com/issue/IDEA-70451

There is a regex Intellij plugin, but alas it also does not support lower and upper-casing.

http://plugins.jetbrains.com/plugin/19?pr=idea

You might just have to run the files through a perl program to replace them correctly.

Solution 3 - Java

I started using Idea Vim plugin and learn to do things like this in Vim. This way I could re-use these skills outside of Idea.

Here is the vim command to do what you asked for.

:%s/private\s\(\w*\)\s\(w*\)/private \1 \L\2/g

Regex being entered within the IDE. The extra slashes are required to escape the regex pattern into the Vim.

Within The IDE

Find Plugin from within the IDE. enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel KaplanView Question on Stackoverflow
Solution 1 - JavadesseimView Answer on Stackoverflow
Solution 2 - JavagaoagongView Answer on Stackoverflow
Solution 3 - JavachintoView Answer on Stackoverflow