高分 正则表达式

weiluo12 2009-12-24 03:00:13
有这样一类代码(方法)

{
conn = DBConnectionPool.getConnection();
//可以有代码
stmt = conn.prepareStatement("update t_base_company set name=?,remark=? " +
"where id=?");
stmt.setString(1, name);
stmt.setString(2, remark);
stmt.setShort(3, id);
stmt.execute();
//可以有代码
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBConnectionPool.closeStatementSafey(stmt);
DBConnectionPool.closeConnectionSafey(conn);
}

现在我想通过eclipse搜索(通过正则表达式的方式)包含getConnection和closeConnectionSafey。
请问这样的正则表达式这样写:关键是换行(不确定有多少行)、空格。这能这类代码中找,不能跨代码 (方法)

谢谢各位!!!
...全文
443 38 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
GSDN00A 2010-03-11
  • 打赏
  • 举报
回复
有趣,回贴到33楼。楼主在34的一声唤下,35楼把分全部接走了。
weiluo12 2010-03-10
  • 打赏
  • 举报
回复
谢谢各位带大侠!

快来接分了...
duchq044 2010-01-15
  • 打赏
  • 举报
回复
学习一下
huangwj20042008 2010-01-15
  • 打赏
  • 举报
回复
public static void method1(){
getConnection();
closeConnectionSafey();
}
public static void getConnection(){

}
public static void closeConnectionSafey(){

}
}
class PosName{
private int position;
private String name;

public PosName(int position,String name){
this.position = position;
this.name = name;
}

public int getPosition() {
return position;
}

public void setPosition(int position) {
this.position = position;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Tassdars 2010-01-15
  • 打赏
  • 举报
回复
顶,学习一下!
huangwj20042008 2010-01-15
  • 打赏
  • 举报
回复
public static String findSpecialText(List<String> fileList,String searchGet,String searchClose){
int fileNum = fileList.size();
String regex = "[a-zA-Z_$][a-zA-Z0-9_$]*(\\s*\\[\\s*\\])?\\s+[a-zA-Z_$][a-zA-Z0-9_$]*\\s*\\((\\s*[a-zA-Z_$][a-zA-Z0-9_$]*(\\s*\\[\\s*\\])?\\s+[a-zA-Z_$][a-zA-Z0-9_$]*(\\s*\\[\\s*\\])?\\s*|\\s*)\\)\\s*\\{";
Pattern pattern = Pattern.compile(regex,Pattern.DOTALL);
StringBuilder methodBuilder = new StringBuilder();
String splitLine = "--------------------";
for(int i=0;i<fileNum;i++){

String tempPath = fileList.get(i);
StringBuilder builder = new StringBuilder();
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(tempPath)));
byte[] buffer = new byte[1024];
int readNum = 0;


while((readNum = bis.read(buffer)) != -1){
builder.append(new String(buffer,0,readNum));
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}

String code = builder.toString();

Matcher matcher = pattern.matcher(code);
ArrayList<PosName> positionList = new ArrayList<PosName>();
while(matcher.find()){
int start = matcher.start();
int end = matcher.end();
String name = code.substring(start,end);
int tempIndex = name.indexOf('{');
name = name.substring(0,tempIndex);
positionList.add(new PosName(start,name));
}
int len = positionList.size();


String methodText = null;
int getIndex = 0;
int closeIndex = 0;
boolean appendFileName = false;
if(len>0){
for(int j=0;j<len-1;j++){
PosName curPosName = positionList.get(j);
PosName nextPosName = positionList.get(j+1);
int curPos = curPosName.getPosition();
int nextPos = nextPosName.getPosition();
methodText = code.substring(curPos,nextPos);
int tempIndex = methodText.lastIndexOf('}');
methodText = methodText.substring(0,tempIndex+1);
getIndex = methodText.indexOf(searchGet);
closeIndex = methodText.indexOf(searchClose);
if(getIndex>=0 && closeIndex>=0){
if(!appendFileName){
methodBuilder.append(splitLine+tempPath+splitLine+"\n");
appendFileName = true;
}
methodBuilder.append(curPosName.getName()+"\n");
}

}
PosName lastPosName = positionList.get(len-1);
methodText = code.substring(lastPosName.getPosition());
methodText = methodText.substring(0,methodText.lastIndexOf('}'));
getIndex = methodText.indexOf(searchGet);
closeIndex = methodText.indexOf(searchClose);
if(getIndex>=0 && closeIndex>=0){
if(!appendFileName){
methodBuilder.append(splitLine+tempPath+splitLine+"\n");
appendFileName = true;
}
methodBuilder.append(lastPosName.getName()+"\n");
}
}
}
return methodBuilder.toString();
}
huangwj20042008 2010-01-15
  • 打赏
  • 举报
回复
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UseReflectFind {
public void method2(){
Method[] methods;
System.out.println();
}
public static void main(String[] args){

getConnection();
closeConnectionSafey();
List<String> fileList = iterateDir("D:\\hwj\\work\\workspace\\Test\\src");
System.out.println("file number is:"+fileList.size());
String searchGet = "getConnection";
String searchClose = "closeConnectionSafey";
System.out.println("such methods contain :\n"+findSpecialText(fileList,searchGet,searchClose));


}
public static List<String> iterateDir(String srcDir){
File srcFile = new File(srcDir);
File[] files = srcFile.listFiles();
ArrayList<String> pathList = new ArrayList<String>();
for(int i=0;i<files.length;i++){
File tempFile = files[i];
if(tempFile.isDirectory()){
pathList.addAll(iterateDir(tempFile.getAbsolutePath()));
}else{
pathList.add(tempFile.getAbsolutePath());
}
}
return pathList;
}
huangwj20042008 2010-01-15
  • 打赏
  • 举报
回复
不支持构造函数,如果构造函数含有这种代码,则寻找不到。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UseReflectFind {
public void method2(){
Method[] methods;
System.out.println();
}
public static void main(String[] args){

getConnection();
closeConnectionSafey();
List<String> fileList = iterateDir("D:\\hwj\\work\\workspace\\Test\\src");
System.out.println("file number is:"+fileList.size());
String searchGet = "getConnection";
String searchClose = "closeConnectionSafey";
System.out.println("such methods contain :\n"+findSpecialText(fileList,searchGet,searchClose));


}
public static List<String> iterateDir(String srcDir){
File srcFile = new File(srcDir);
File[] files = srcFile.listFiles();
ArrayList<String> pathList = new ArrayList<String>();
for(int i=0;i<files.length;i++){
File tempFile = files[i];
if(tempFile.isDirectory()){
pathList.addAll(iterateDir(tempFile.getAbsolutePath()));
}else{
pathList.add(tempFile.getAbsolutePath());
}
}
return pathList;
}
weiluo12 2010-01-14
  • 打赏
  • 举报
回复
... ...
fhuibo212 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 xiezhifu 的回复:]
或者楼主的意思是下面的这样子?
先定位代码块的起始位置“{”,然后分别查找getConnection或closeConnectionSafey这两个字段,如果先找到的是getConnection字段,那下一个查找就找closeConnectionSafey字段;如果是先找到closeConnectionSafey字段,那下一个就查找getConnection字段。如果碰到代码块结束符“}”或者碰到代码块起始符“{”则重新初始化查找进行下一个代码块查找,如果没有碰到代码块结束符“}”或者没有碰到代码块起始符“{”并且同时找到这两个字段,那就进行处理。
如果楼主的意思是上面的这两种意思,可以按照思路进行设计查找方法了
[/Quote]

如果第二个字段是在finally{}里??
laker_914 2010-01-14
  • 打赏
  • 举报
回复
想都不敢想,呵呵
fhuibo212 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cantona1994 的回复:]
LZ的头像是亮点
[/Quote]
你的头像也挺靓
fhuibo212 2010-01-14
  • 打赏
  • 举报
回复
没碰过这样的问题,值得研究~
wekui 2009-12-26
  • 打赏
  • 举报
回复
up
xiezhifu 2009-12-26
  • 打赏
  • 举报
回复
或者楼主的意思是下面的这样子?
先定位代码块的起始位置“{”,然后分别查找getConnection或closeConnectionSafey这两个字段,如果先找到的是getConnection字段,那下一个查找就找closeConnectionSafey字段;如果是先找到closeConnectionSafey字段,那下一个就查找getConnection字段。如果碰到代码块结束符“}”或者碰到代码块起始符“{”则重新初始化查找进行下一个代码块查找,如果没有碰到代码块结束符“}”或者没有碰到代码块起始符“{”并且同时找到这两个字段,那就进行处理。
如果楼主的意思是上面的这两种意思,可以按照思路进行设计查找方法了
xiezhifu 2009-12-26
  • 打赏
  • 举报
回复
先定位代码块的起始位置“{”,然后分别查找getConnection或closeConnectionSafey这两个字段,如果先找到的是getConnection字段,那下一个查找就找closeConnectionSafey字段;如果是先找到closeConnectionSafey字段,那下一个就查找getConnection字段。如果碰到代码块结束符“}”则重新初始化查找进行下一个代码块查找,如果没有碰到代码块结束符“}”并且同时找到这两个字段,那就进行处理。
不知道楼主的意思是不是这样?
bobo415 2009-12-26
  • 打赏
  • 举报
回复
没有见过
牛叔 2009-12-25
  • 打赏
  • 举报
回复
还是么搞懂
。。。。
py330316117 2009-12-25
  • 打赏
  • 举报
回复
lz是想说用正则表达式作为条件然后在数据库中进行查询?
  • 打赏
  • 举报
回复
祝福下
加载更多回复(15)

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧