Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 379
Daniel Kaplan Avatar asked Jun 10 '14 20:06

Daniel Kaplan


People also ask

Can I use regex in replace?

How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.


2 Answers

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.

like image 111
desseim Avatar answered Oct 05 '22 12:10

desseim


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.

like image 31
gaoagong Avatar answered Oct 05 '22 12:10

gaoagong