Java 如何使用正则表达式在 Intellij IDEA 中用小写替换大写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24149994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use a regex to replace upper case with lower case in Intellij IDEA?
提问by Daniel Kaplan
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 $L
with: private \L
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.
他们都没有工作。是否可以在 intellij 中执行此操作,还是缺少此功能?这仅用于教育目的,示例是人为设计的。我只想知道这是否可以在 intellij 中做到。
采纳答案by desseim
In IDEA 15you're able to use the below switches to toggle the case of captured expressions. This is now officially documentedsince this version was released.
在 IDEA 15 中,您可以使用以下开关来切换捕获表达式的大小写。自从此版本发布以来,现在正式记录了这一点。
\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
\l
: 降低下一个字符的大小写\u
: up 下一个字符的大小写\L
: 降低下一个字符的大小写,直到 a\E
或替换字符串的结尾\U
: up 下一个字符的大小写,直到 a\E
或替换字符串的结尾\E
: 标记由\U
或发起的案例更改的结束\L
Here is an example usage (as the documentation is not clear):
这是一个示例用法(因为文档不清楚):
find: (\w+_)+(\w+) replace: \L$1$2\E
查找:(\w+_)+(\w+) 替换:\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.
以上将转换FOO_BAR_BAZ
为foo_bar_baz
etc $1 指的是第一个找到的捕获组(括号中),$2 指的是第二组,以此类推。
For posterity's sake: this was initially reportedby @gaoagong and documented there.
回答by gaoagong
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.
搜索答案,然后意识到@ajp15243 已经在上面回答了这个问题。Intellij 目前无法使用其正则表达式替换功能来更改字母的大小写。以下 URL 上有关于该功能的简短讨论。
http://www.jetbrains.com/idea/webhelp/regular-expression-syntax-reference.html
http://www.jetbrains.com/idea/webhelp/regular-expression-syntax-reference.html
You can also vote for the feature in the Youtrack issue here:
您还可以在此处为 Youtrack 问题中的功能投票:
http://youtrack.jetbrains.com/issue/IDEA-70451
http://youtrack.jetbrains.com/issue/IDEA-70451
There is a regex Intellij plugin, but alas it also does not support lower and upper-casing.
有一个正则表达式 Intellij 插件,但可惜它也不支持大写和小写。
http://plugins.jetbrains.com/plugin/19?pr=idea
http://plugins.jetbrains.com/plugin/19?pr=idea
You might just have to run the files through a perl program to replace them correctly.
您可能只需要通过 perl 程序运行文件即可正确替换它们。
回答by chinto
I started using Idea Vimplugin and learn to do things like this in Vim. This way I could re-use these skills outside of Idea.
我开始使用Idea Vim插件并学习在 Vim 中做这样的事情。这样我就可以在 Idea 之外重用这些技能。
Here is the vim command to do what you asked for.
这是执行您要求的 vim 命令。
:%s/private\s\(\w*\)\s\(w*\)/private \L/g
Regex being entered within the IDE. The extra slashes are required to escape the regex pattern into the Vim.
在 IDE 中输入正则表达式。需要额外的斜线才能将正则表达式模式转义到 Vim 中。
Find Plugin from within the IDE.
从 IDE 中查找插件。