lyyb2001

只是为方便自己找记录而已
posts - 57, comments - 27, trackbacks - 0, articles - 5
  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理

正则表达式匹配字符串

Posted on 2015-03-20 09:41 skycity 阅读(213) 评论(0)  编辑  收藏 所属分类: J2SE技术
有这样一个串
Read([SQLServer#10.5.219.21#mas_db],[select * from material where machine_seq='!dbo.repair.machine_seq' and b='@@dbo.test.cc' and c='!!dbo.test.dd' and d='@dbo.repair' and e='sd.bf' and f='@aa.bg' and g='#dbo.repair.machine_name' and h=@dbo.repair.owner],[en_US])
需要解析中间的select * from material where machine_seq='!dbo.repair.machine_seq' and b='@@dbo.test.cc' and c='!!dbo.test.dd' and d='@dbo.repair' and e='sd.bf' and f='@aa.bg' and g='#dbo.repair.machine_name' and h=@dbo.repair.owner
还需要满足'@@','@','!'(前后包含单引号),的变量取出来
@schema.table.fieldName     @dbo.repair.machine_seq要点:获取界面上某个控件录入的值;
#schema.table     #dbo.reapir要点:获取这个表的记录行数;
!schema.table.fieldName   !dbo.repair.qty要点:统计dbo.repair表qty总和. 相当于Sum(qty)性质.
@@constName   @@User_ID要点:获取内存变量值;

实现如下:
package com.lenovo.nabf.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NabfUtilTest {
    public static final Pattern PatternSql = Pattern.compile("\\'(([#!]{1}|[@]{1,2})[\\w\\.]+?)\\'"); // match '@dbo.repair.machine_seq'
    public static final Pattern PatternReadString = Pattern.compile(".+?,\\[([\\w\\.\\s{*!@#=\'}]+?)\\],.+?");  //match Read([db],[sqlstring],[lang])得到sqlstring的值
    public static void main(String args[]){
         String readString="Read([SQLServer#10.5.219.21#mas_db],[select * from material where "
            + "machine_seq='!dbo.repair.machine_seq' and b='@@dbo.test.cc' "
            + "and c='!!dbo.test.dd' and d='@dbo.repair' and e='sd.bf' and f='@aa.bg' "
            + "and g='#dbo.repair.machine_name' and h=@dbo.repair.owner],[en_US])"; 
        System.out.println(readString);
        System.out.println(analysisReadStr(readString));
        List list=NabfUtil.getRegexMatchedList(readString);
        for(String s:list){
             System.out.println(s);
        }
        String sqlString = NabfUtil.getStringByAnalysisReadString(readString);
        System.out.println(sqlString);
    } 
    public static ArrayList getRegexMatchedList(String readString){
        String sqlString = analysisReadStr(readString);
        ArrayList matchList=new ArrayList();
        Matcher matcher = PatternSql.matcher(sqlString);
        int lastEndIndex = 0;
        while (matcher.find(lastEndIndex)) {
            matchList.add(matcher.group(1));
            lastEndIndex = matcher.end();
        }
        return matchList;
    }
    public static String getStringByAnalysisReadString(String readString){
        String sqlString = analysisReadStr(readString);
        Matcher matcher = PatternSql.matcher(sqlString);
        StringBuilder sb = new StringBuilder();
        int lastEndIndex = 0;
        while (matcher.find(lastEndIndex)) {
            sb.append(sqlString.substring(lastEndIndex, matcher.start()));
            lastEndIndex = matcher.end();
        }
        sb.append(sqlString.substring(lastEndIndex));
        return sb.toString();
    }
    private static String analysisReadStr(String readString){
        Matcher matcher = PatternReadString.matcher(readString);
        int lastEndIndex = 0;
        String sqlString="";
        while (matcher.find(lastEndIndex)) {
                 sqlString=matcher.group(1);
                 lastEndIndex = matcher.end();
        }
        return sqlString;
   }
}



Lyyb2001

只有注册用户登录后才能发表评论。


网站导航: