Regex 有没有办法抓取第一个单词并替换引用的部分? [英] Is there a way with Regex to grab the first word and replace a quoted section?

查看:44
本文介绍了Regex 有没有办法抓取第一个单词并替换引用的部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ACACIA_BOAT(复制内容时只是一个占位符)替换为第一个词,即变量名.

I'm trying to replace ACACIA_BOAT (just a placeholder when copying things) with the first word, that being the variable name.

SCAFFOLDING = new ItemBuilder(Material.valueOf("ACACIA_BOAT")).build();
SEAGRASS = new ItemBuilder(Material.valueOf("ACACIA_BOAT")).build();
SKULL_BANNER_PATTERN = new ItemBuilder(Material.valueOf("ACACIA_BOAT")).build();

// into this

SCAFFOLDING = new ItemBuilder(Material.valueOf("SCAFFOLDING")).build();
SEAGRASS = new ItemBuilder(Material.valueOf("SEAGRASS")).build();
SKULL_BANNER_PATTERN = new ItemBuilder(Material.valueOf("SKULL_BANNER_PATTERN")).build();

只是一个例子.课程中有数千个条目.

Just an example. There's thousands of entries across classes.

有什么方法可以用 Regex 简化这个过程?

Any way to simplify this process with Regex?

如果有帮助,我正在使用 IntelliJ.

I'm using IntelliJ if that helps.

推荐答案

如何在Intellij项目中使用正则表达式查找和替换文本

在本例中使用了 Intellij 2020.2.

In this case Intellij 2020.2 was used.

您会在菜单下找到替换工具:

You will find the replacement tool under menu:

编辑/查找/'在路径中替换...'

或走捷径:

Ctrl+Shift+R

在搜索窗口中输入:

(\w+) = new ItemBuilder\(Material\.valueOf\(\\"ACACIA_BOAT\\"

在替换窗口类型中:

$1 = new ItemBuilder\(Material\.valueOf\(\\"$1\\"

确保选择以下内容:

  • Match case 'Aa' - 位于搜索窗口"的右侧;(激活时亮蓝色)

  • Match case 'Aa' - found on the right hand side of "search window" (It is lit with blue color when it is activated)

Regex '.*' - 位于搜索窗口"的右侧;(激活时亮蓝色)

Regex '.*' - found on the right hand side of "search window" (It is lit with blue color when it is activated)

在项目中" - 位于左侧替换窗口"下方

'In Project' - found on the left hand side just under the "replacement window"

设置完成后点击全部替换"按钮执行请求.

After settings click button 'Replace All' to execute request.

下面是工具"的图片,使用请求的正则表达式:

Below is a picture of the "tool", using requested regex:

阅读有关 Intellij 的查找并替换为正则表达式"的更多信息;在:

Read more about Intellij's "Find and replace with regex" at:

https://www.jetbrains.com/help/phpstorm/tutorial-finding-and-replacing-text-using-regular-expressions.html

这篇关于Regex 有没有办法抓取第一个单词并替换引用的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆