如何在同一个正则表达式中使用捕获的组 [英] 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 ->查找 ->在路径中替换
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
更新
L
是一个错字.它应该是 \L
根据 JetBrains 的文档
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 "=":
要查找的文本:
m([A-Z])([A-Za-z0-9]+) = (L\1\2)
m([A-Z])([A-Za-z0-9]+) = (L\1\2)
替换为:
this.L$1$2 = $3
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)
和这个替换字符串:
this\.L$1$2 = $3
因此,对于带有输入文本的示例:
So with your example with an input text:
mContext = context
你得到这个:
this.LContext = context
我不知道在您的文本/替换字符串中指定的L"是您的拼写错误还是其他错误,但如果是这样,您可以通过以下方式更改替换字符串":
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
所以你可以得到这个:
this.context = context
如果这对您有帮助,请告诉我!
Let me know if this help you!
这篇关于如何在同一个正则表达式中使用捕获的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!