如何在同一个正则表达式中使用捕获的组 [英] How can I use the captured group in the same regex
问题描述
mFoo = foo;
mBar = bar;
// convert to
this.foo = foo;
this.bar = bar;
如何使用正则表达式来处理这种替换?请帮忙.这是我在Android Studio(IntelliJ IDEA)Edit -> Find -> Replace in Path
Text to find: m([A-Z])([A-Za-z0-9]+) = L$1$2
Replace with: this\.L$1$2 = L$1$2
更新
上面的 L
是一个错字.根据 JetBrains的文档,它应该为\L
您可以使用带有反向引用的模式,并将"=:"后面的最后一个单词分组
要查找的文本:
m([A-Z])([A-Za-z0-9] +)=(L \ 1 \ 2)
替换为:
this.L $ 1 $ 2 = $ 3
在您发表评论后,我了解到您在使用小写/大写字符时也遇到了问题.因此,请尝试这种模式(我也简化了正则表达式):
m(\p{Alpha})(\w+) = (((?i)\1)\2)
和此替换字符串:
this\.L$1$2 = $3
在您的示例中输入文本:
mContext = context
您获得了:
this.LContext = context
我不知道您的文本/替换字符串中指定的"L"是您的错字错误还是其他错误,但是如果是这样,您可以通过以下方式更改替换字符串":
this\.$3 = $3
因此您可以获取此信息:
this.context = context
让我知道这是否对您有帮助!
mFoo = foo;
mBar = bar;
// convert to
this.foo = foo;
this.bar = bar;
How to use a regex to handle this substitution? Please help. Here is the method I used in Android Studio (IntelliJ IDEA) Edit -> Find -> Replace in Path
Text to find: m([A-Z])([A-Za-z0-9]+) = L$1$2
Replace with: this\.L$1$2 = L$1$2
Update
L
above is a typo. It should be \L
according to JetBrains' document
You can use a pattern with back-reference and grouping the last words after "=":
Text to find:
m([A-Z])([A-Za-z0-9]+) = (L\1\2)
Replace with:
this.L$1$2 = $3
After your comments, I understand that you have also problems with lowercase/uppercase characters. So try this pattern (I have also simplified the regex):
m(\p{Alpha})(\w+) = (((?i)\1)\2)
and this replace string:
this\.L$1$2 = $3
So with your example with an input text:
mContext = context
you obtain this:
this.LContext = context
I don't know if "L" specified in your text/replace string is your typo error or other but if it's so you can change the "replace string" in the following way:
this\.$3 = $3
So you can obtain this:
this.context = context
Let me know if this help you!
这篇关于如何在同一个正则表达式中使用捕获的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!