使用Java解析Java代码
是否可以使用正则表达式解析一些 Java代码?

所以我想说我想要一个int变量列表:

int abc1 = 1;
int abc2 = abc1 + 1;
int abd3 = abc1 + abc2;

我想将它们放入ArrayList中.

所以像这样:

private void parse(String s){

    List<List<String>> variables = new ArrayList<List<String>>();

    list.add(new ArrayList<String>);//var type
    list.add(new ArrayList<String>);//var name
    list.add(new ArrayList<String>);//var data

    Pattern p = Pattern.compile();//This is what I want
    Matcher m = p.matcher(s);

    while(m.find()){
        String match = m.group();
        Pattern p2 = Pattern.compile();//Here as well
        Matcher m2 = p.matcher(s);
        while(m2.find()){
            for(int i = 0; i < m.groupCount()){
                //add the variables to the lists
            }
        }
    }
}

我要问的是正则表达式可以处理这个任务?

所有这一切的原因是用户可以使用一些代码(Android应用程序btw)对应用程序进行一些控制

如果不建议使用正则表达式,那么我应该使用什么解析器?

最佳答案
人们经常尝试用正则表达式解析HTML,XML,C或java.

通过足够的努力和技巧,复杂的正则表达式组合可以实现许多令人惊叹的事物.但是你总是得到一些非常不完整而且效率不高的东西.

正则表达式无法处理复杂的语法,使用解析器,无论是泛型还是specific to java.

点击查看更多相关文章

转载注明原文:使用Java解析Java代码 - 乐贴网