Java IntelliJ 中的 RegEx 反向引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1421797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RegEx backreferences in IntelliJ
提问by Dónal
I want to use IntelliJ's find-and-replace feature to perform the following transformation:
我想使用 IntelliJ 的查找和替换功能来执行以下转换:
// Replace this
model.put('foo', 'bar')
// With this
model['foo'] = bar
I've tried the following:
我尝试了以下方法:
Text to find: model.put\((.*),(.*)\)
Replace with: model\[\\1\] = \\2
要查找的文本:model.put\((.*),(.*)\)
替换为:model\[\\1\] = \\2
But Intellij doesn't seem to recognise \\1
and \\2
as backreferences. I've also tried a single slash, but that doesn't work either.
但是 Intellij 似乎没有识别\\1
和\\2
作为反向引用。我也试过一个斜线,但这也不起作用。
采纳答案by Steve K
IntelliJ uses $1
for replacementbackreferences.
IntelliJ$1
用于替换反向引用。
From IntelliJ's help:
来自 IntelliJ 的帮助:
For more information on regular expressions and their syntax, refer to documentation for java.util.regexBack references should have $n, rather than \n format.
有关正则表达式及其语法的更多信息,请参阅java.util.regex文档 后向引用应具有 $n,而不是 \n 格式。
回答by Cong De Peng
IntelliJ IDEA?/? Reference?/? Regular Expression Syntax Reference
IntelliJ IDEA?/? 参考?/?正则表达式语法参考
Matches subexpression and remembers the match. If you need to use the matched substring within the same regular expression, you can retrieve it using the backreference (\num, where num = 1..n). If you need to refer the matched substring somewhere outside the current regular expression (for example, in another regular expression in the Replacement field), you can retrieve it using the dollar sign ($num, where num = 1..n). If you need to include the parentheses characters into the subexpression, use "(" or ")".
匹配子表达式并记住匹配项。如果您需要在同一个正则表达式中使用匹配的子字符串,您可以使用反向引用(\num,其中 num = 1..n)检索它。如果您需要在当前正则表达式之外的某处引用匹配的子字符串(例如,在替换字段中的另一个正则表达式中),您可以使用美元符号($num,其中 num = 1..n)检索它。如果需要在子表达式中包含括号字符,请使用“(”或“)”。
回答by Glen
The in-product contextual help for regex in Idea 9.0 (and perhaps other versions) appears to be incorrect. It states this:
Idea 9.0(可能还有其他版本)中正则表达式的产品内上下文帮助似乎不正确。它指出:
Back references \n Whatever the nth capturing group matched
But apparently, as mentioned in previous answers and is my experience, it's really \$n for back references, rather than \n
但显然,正如之前的答案中提到的,也是我的经验,它实际上是 \$n 用于反向引用,而不是 \n
You get to this contextual help by clicking the '[Help]' link next to the "Regular expression" radio option on the the "Replace Text" dialog box
您可以通过单击“替换文本”对话框中“正则表达式”单选选项旁边的“[帮助]”链接来获取此上下文帮助
回答by Barett
In short, you must use $1
to $n
for replacementbackreferences. \1
syntax is only for backreferences within the search.
简而言之,您必须使用$1
to$n
来替换反向引用。\1
语法仅用于搜索中的反向引用。
In IntelliJ 2016, the in-app documentation is misleading. Here is a better quote from the full docs:
在 IntelliJ 2016 中,应用内文档具有误导性。这是完整文档中更好的引用:
If you need to refer the matched substring somewhere outside the current regular expression (for example, in another regular expression as a replacement string), you can retrieve it using the dollar sign ($num, where num = 1..n).
如果您需要在当前正则表达式之外的某处引用匹配的子字符串(例如,在另一个正则表达式中作为替换字符串),您可以使用美元符号($num,其中 num = 1..n)检索它。