正则表达式找到静态(非最终)变量 [英] Regex to find static (non final) variables
问题描述
我正在尝试在Eclipse(Java)工作区中进行搜索,以查找非最终的静态变量的所有实例。
I am trying to do a search in my Eclipse (Java) workspace to find all instances of static variables that are not final.
我尝试了各种正则表达式但是他们不要导致任何匹配。有人可以建议一个正则表达式匹配包含 static
并且不包含 final
的所有行,而不是以<$结尾c $ c> {?
I tried various regexes but they do not result in any matches. Can someone suggest a regex that will match all lines containing static
and not containing final
, and not ending in a {
?
关于不以 {
结尾的最后一部分将消除静态方法。
The last part about not ending with a {
will eliminate static methods.
例如:
public class FlagOffendingStatics {
private static String shouldBeFlagged = "not ok";
private static final String ok = "this is fine";
public static void methodsAreOK() {
}
}
推荐答案
此模式有效:
[^(final)] static [^(final)][^(\})]*$
这是一个测试:
$ cat test.txt
private int x = "3";
private static x = "3";
private final static String x = "3";
private static final String x = "3";
private static String x = "3";
public static void main(String args[]) {
blah;
}
$ grep "[^(final)] static [^(final)][^(\})]*$" test.txt
private static x = "3";
private static String x = "3";
(我意识到私有静态x =3;
是无效的语法,但模式仍然正常。)
(I realize that private static x = "3";
isn't valid syntax, but the pattern still holds ok.)
该模式解释了 final
可以在 static
之前或之后出现 [^(final)] static [^(final)]
。模式的其余部分 [^(\})] * $
,旨在防止任何 {
个字符出现在该行的其余部分。
The pattern accounts for the fact that final
can appear before or after static
with [^(final)] static [^(final)]
. The rest of the pattern, [^(\})]*$
, is meant to prevent any {
characters from appearing in the remainder of the line.
如果有人喜欢这样编写方法语句,则此模式不起作用:
This pattern will not work however if anyone likes to write their method statements like this:
private static void blah()
{
//hi!
}
这篇关于正则表达式找到静态(非最终)变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!