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

发布于 2024-08-21 16:42:49 字数 202 浏览 4 评论 0原文

我想检查所有源代码文件,并将每次出现的 k_Xyyy 替换为 k_xyyy (将 k_ 之后的第一个字母从大写转小写)。

我正在使用 Eclipse 对话框来搜索和替换多个文件。现在我有正则表达式 \bk_([AZ])

如何指定正则表达式的替换字符串?

I'd like to go through all of my source code files and replace every occurence of k_Xyyy with k_xyyy (switch the first letter after k_ from uppercase to lowercase).

I'm using the eclipse dialog to search and replace multiple files. Right now I have the regex \bk_([A-Z]).

How do I specify the replacement string of the regex?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗恋未遂 2024-08-28 16:42:51

我刚刚利用 VIM 的强大功能解决了相同的任务(必须将 .net 接口转换为 java 接口):)

void DoMethod1 -> void doMethod1
Foo PerformMethod2 -> Foo performMethod2
:%s/\(^\s*\w\+\s\+\)\([A-Z]\)/\1\L\2/g

这里我们正在搜索(可选缩进,后跟返回类型,后跟空格),后跟(大写字母)。大括号是捕获组。然后我们执行替换第一个捕获组 \1 小写 \L 第二个捕获组 \2。

这当然需要您在 Vim 中打开文件,但无论如何,这比在 Eclipse 中手动执行相同的操作要快得多。

I just resolved the same task (had to turn .net interface into java interface) utilizing the power of VIM :)

void DoMethod1 -> void doMethod1
Foo PerformMethod2 -> Foo performMethod2
:%s/\(^\s*\w\+\s\+\)\([A-Z]\)/\1\L\2/g

Here we are searching for (optional indentation followed by return type followed by whitespace) followed by (Uppercase letter). Braces are capturing groups. Then we are performing a replacement first capturing group \1 lowercase \L second capturing group \2.

This of course requires you to open file in Vim, but anyway this is much faster then doing the same thing by hand in Eclipse.

沐歌 2024-08-28 16:42:51

那是不可能的。要么使用 Eclipse 的重构功能,要么一次替换它们:

regex       : \bk_A
replacement : k_a 

regex       : \bk_B
replacement : k_b 

...

regex       : \bk_Z
replacement : k_z 

That is not possible. Either use Eclipse's re-factoring functionality, or replace them one at a time:

regex       : \bk_A
replacement : k_a 

regex       : \bk_B
replacement : k_b 

...

regex       : \bk_Z
replacement : k_z 
相权↑美人 2024-08-28 16:42:51

我需要对大量源代码执行此操作,其中字符串文字需要转换为小写。我找到了一种使用 Notepad++ 和 Python 脚本插件的方法,如使用 在这里

I needed to do this for a huge chunk of source code where string literals needed to be converted to lowercase. I found a way using Notepad++ and the Python Script plugin, as used here.

作死小能手 2024-08-28 16:42:51

(对于我来说,因为我刚刚开始编程,所以思考这个更有趣)
获取 $pattern_to_change 并使用 ord() 将其从 ASCII 转换为十进制。获取生成的十进制数并添加 32。然后将 $desired_pa​​ttern 转换回来使用chr()转换为ascii。

或者只需下载 SublimeText 并使用其查找和替换功能来查找所有出现的内容并用不同的文本替换它们(Sublime 也有正则表达式)。

我确信您可以手动转换 10 亿美元,因为这篇文章已经有 5 年历史了,但您可以使用 Sublime 在 5 分钟内完成此操作。

非常有用的文本编辑器。

(for me, since I just started programming, this was more fun to think about)
Take $pattern_to_change and convert it from ascii to decimal using ord().Take the resulting dec number and add 32. Then convert $desired_pattern back to ascii using chr().

Or just download SublimeText and use its Find and Replace feature to Find All occurrences and replace them with difference text (Sublime has regex as well).

I'm sure you could have converted one billion by hand since this post is like 5 years old, but you could have complete this in 5 minutes with Sublime.

Really useful text editor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文