在 Intellij IDEA,如何使用正则表达式将大写字母替换为小写字母?

我在谷歌上搜索了一下,发现了如何使用其他正则表达式解析器:

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

我已经尝试过这些方法,但是都没有用:

private String Name;
private Integer Bar = 2;

这样说:

private String name;
private Integer bar = 2;

我试过这样的方法:

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

都不管用。有可能在 intellij 中做到这一点吗,或者这是一个缺失的特性?这只是为了教育的目的,这个例子是人为的。我只是想知道这是否可行。

30046 次浏览

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.

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

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.