是否有可能用正则表达式解析一些Java代码?
假设我想要一个int变量的列表:
int abc1 = 1;
int abc2 = abc1 + 1;
int abd3 = abc1 + abc2;我想把这些放到ArrayList中。
所以像这样:
private void parse(String s){
List> variables = new ArrayList>();
list.add(new ArrayList);//var type
list.add(new ArrayList);//var name
list.add(new ArrayList);//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)对应用程序进行一些控制,
如果不推荐使用正则表达式,那么我应该使用哪种解析器?