lyyb2001

          只是為方便自己找記錄而已
          posts - 57, comments - 27, trackbacks - 0, articles - 5
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理

          正則表達式匹配字符串

          Posted on 2015-03-20 09:41 skycity 閱讀(228) 評論(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
          主站蜘蛛池模板: 名山县| 崇明县| 揭东县| 隆德县| 吴旗县| 西丰县| 夹江县| 道真| 上栗县| 长汀县| 南宁市| 四川省| 邵阳市| 阳新县| 灌阳县| 渭南市| 惠州市| 新丰县| 靖江市| 城步| 景宁| 海南省| 修武县| 鄯善县| 建始县| 澎湖县| 奉节县| 翼城县| 崇州市| 宁武县| 临沂市| 贵南县| 石楼县| 资兴市| 青海省| 凌源市| 缙云县| 淮北市| 宁德市| 上虞市| 金乡县|