英文:
give a value to captured group and use it in replace function of intellij
问题
以下是翻译好的部分:
我正在尝试在IntelliJ中使用正则表达式来翻译我的一些方法和字段名称。特别是我试图实现类似于这样的效果:
public class SomeClass {
private String italianNameField;
public void setItalianNameField(String italianNameField) {
this.italianNameField = italianNameField;
}
}
变成
public class SomeClass {
private String englishNameField;
public void setEnglishNameField(String englishNameField) {
this.englishNameField = englishNameField;
}
}
到目前为止,我的正则表达式如下所示:
`(public void set)ItalianFieldName(\(String )italianFieldName(\) \{ this.)italianFieldName( = )italianFieldName(; \})`,并且在替换字段中我放置了`$1EnglishNameField$2englishNameField$3englishNameField$4englishNameField$5`
我想知道是否可以创建一个变量并使用它,就像这样:
var1 = englishName
public void set\Lvar1(String var1) { this.var1 = var1; }
并且可能同时更改字段名称
英文:
I'm trying to translate some of my methods and field names with regex in intellij. In particular I'm trying to achieve something like this:
public class SomeClass {
private String italianNameField;
public void setItalianNameField(String italianNameField) {
this.italianNameField = italianNameField;
}
}
becomes
public class SomeClass {
private String englishNameField;
public void setEnglishNameField(String englishNameField) {
this.englishNameField = englishNameField;
}
}
so far my regex look like this:
(public void set)ItalianFieldName(\(String )italianFieldName(\) \{ this.)italianFieldName( = )italianFieldName(; \})
and in the replace field I put $1EnglishNameField$2englishNameField$3englishNameField$4englishNameField$5
I would like to know if it's possible to do create a variable and use that, something like this:
var1 = englishName
public void set\Lvar1(String var1) { this.var1 = var1; }
and possibly change the field name at the same time
答案1
得分: 1
-
将其重命名为
englishNameField
,然后按 Enter 键 -
点击确定
英文:
Another approach to achieve the desired result:
答案2
得分: 0
You can replace:
italiannamefield\b
...with:
englishNameField
\b
表示单词边界。IntelliJ 默认情况下以不区分大小写的方式匹配正则表达式,启用 保留大小写 选项(Alt + E)后,它也会正确处理大小写部分。
正则表达式匹配会影响所有内容,从代码到注释和文本。但请注意,这是容易出错的,不建议使用,除非您愿意手动检查所有替换。
尝试一下:
英文:
You can replace:
italiannamefield\b
...with:
englishNameField
\b
means a word boundary. IntelliJ matches regex case-insensitively by default; it will also handle the casing part correctly for you with the Preserve case option (<kbd>Alt</kbd> + <kbd>E</kbd>) enabled.
Regex matching affects everything, from code to comment and text. However, do note that this is error-prone and is not recommended unless you're willing to manually check all replacements.
Try it:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论