lyyb2001

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

          正則表達式匹配字符串

          Posted on 2015-03-20 09:41 skycity 閱讀(221) 評論(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
          主站蜘蛛池模板: 商城县| 惠水县| 黄大仙区| 德令哈市| 郧西县| 迭部县| 昭觉县| 邵阳县| 望谟县| 佛山市| 资溪县| 南靖县| 保亭| 兴仁县| 溧阳市| 涞水县| 天峨县| 太原市| 蒲城县| 枞阳县| 深水埗区| 商河县| 丁青县| 基隆市| 绥中县| 西平县| 邻水| 康乐县| 海淀区| 杭州市| 阜宁县| 湖口县| 安溪县| 沂南县| 临安市| 崇礼县| 德钦县| 保靖县| 余姚市| 英超| 扶绥县|