简体   繁体   English

Java注释行上所有出现的正则表达式(在Eclipse中)

[英]Regex for all ocurrences on Java Commented Lines (in Eclipse)

I need to replace all occurrences of abc with xyz , but only if they occur in Javadoc comments. 我需要用xyz替换所有出现的abc ,但xyz是它们出现在Javadoc注释中。

I am using Eclipse. 我正在使用Eclipse。

I believe requiring simply that the line starts either with \\s*\\* or \\s/\\* should be sufficient for my purposes (no need to match for "occurs between /** and */ ", but that would work fine too). 我相信需要简单地认为行启动要么\\s*\\*\\s/\\* 足以满足我的目的(无需匹配“之间发生/***/ ”,但会工作也没关系)。

I tried using: (\\s\\*|\\s/\\*)(.*)abc and replacing with $1$2xyz but the problem is that the regex is greedy so it gives me only the longest line that matches my string (ie, I can only replace the last abc on any given line). 我尝试使用: (\\s\\*|\\s/\\*)(.*)abc并替换为$1$2xyz但是问题是正则表达式很贪婪,因此它只给我最长匹配我字符串的行(即,我只能替换任何给定行上的最后一个 abc )。 Is there any way to ask it for all the possibilities, or is that outside of the scope of (non-recursive) regular expressions? 有什么方法可以要求所有可能性吗?还是在(非递归)正则表达式的范围之外?

If it is outside the scope, would a recursive expression work? 如果超出范围,则递归表达式可以工作吗? (Which Eclipse does not support in any case). (在任何情况下都不支持Eclipse)。

To my knowledge this is not possible. 据我所知这是不可能的。 A regex traverses each character of the input text once and it can't go back. 正则表达式会遍历输入文本的每个字符一次,并且不能返回。

You can either match on the first abc or the last abc of a JavaDoc line, because by the time it has matched on either of them, the characters defining that line as a JavaDoc comment can't be matched again. 您可以在JavaDoc行的第一个abc或最后一个abc上进行匹配,因为到它们中的任何一个匹配时,定义该行作为JavaDoc注释的字符都无法再次匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM