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

28,962

Solution 1

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

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

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

Share:
28,962

Related videos on Youtube

Rana Ghosh
Author by

Rana Ghosh

Updated on April 20, 2020

Comments

  • Rana Ghosh
    Rana Ghosh over 2 years

    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.

    • ajp15243
      ajp15243 over 8 years
      Is there a reason you want to use a regex? IntelliJ does have a hotkey shortcut for doing that.
    • Rana Ghosh
      Rana Ghosh over 8 years
      @ajp15243 This is just for educational purposes and the example is contrived. I literally want to know if this is possible to do in intellij.
    • ajp15243
      ajp15243 over 8 years
      Fair enough! Just wanted to make sure. I found the IntelliJ Regex Syntax reference, and in the comments, someone looks to ask the same question as you. A moderator answered (and I think slightly mis-understood), but the answer indicates that this isn't possible with IntelliJ's regex syntax.
    • Meo
      Meo over 8 years
      You could always write a plugin for that.
  • Rana Ghosh
    Rana Ghosh over 7 years
    Huge workaround that probably won't help 99% of the people here as they won't want to use VIM emulation. But, I happen to already have this plugin installed so it works for me :)
  • chinto
    chinto over 7 years
    I know.. Vi and Vim seem like evil useless at the start to anyone using an IDE. I eventually started learning Vi after encountering them all in servers. I kinda like it now that I don't have to switch mindsets as I move between tools.
  • Rana Ghosh
    Rana Ghosh over 7 years
    I'm just getting started with it, not fluent yet. Do you think you're more productive editing in Intellij-IDEA with IDEAVim than before you used Vi?
  • chinto
    chinto over 7 years
    More and more.. I used to be good with eclipse. Then moved to Intellij and learned most of it's tricks. Then realised learning Vim would make me good in Server and Workstation at once. I'm also seeing that Vim is far more powerful in text manipulation and navigation. I still use IDE shortcuts for all the IDE specialities like refactoring and finding references and go to def etc. It's very to toggle between the modes though.
  • Mr. Lance E Sloan
    Mr. Lance E Sloan over 7 years
    This is an excellent solution for people who know how to use vi. I switch on IdeaVim when I need to do complex substitutions, then back off again to enjoy Idea's own editor features. I wish IdeaVim had an indicator to show when it is active, though.
  • Kevin Sheehan
    Kevin Sheehan about 7 years
    According to the youtrack, this is being added in version 15 of IntelliJ
  • Vincent Mimoun-Prat
    Vincent Mimoun-Prat 11 months
    For whoever may land here while searching for a solution in PHPStorm, this does not work yet. I have opened a ticket to get support in PHPStorm though.