lyyb2001

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

          正則表達(dá)式匹配字符串

          Posted on 2015-03-20 09:41 skycity 閱讀(221) 評論(0)  編輯  收藏 所屬分類: J2SE技術(shù)
          有這樣一個(gè)串
          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要點(diǎn):獲取界面上某個(gè)控件錄入的值;
          #schema.table     #dbo.reapir要點(diǎn):獲取這個(gè)表的記錄行數(shù);
          !schema.table.fieldName   !dbo.repair.qty要點(diǎn):統(tǒng)計(jì)dbo.repair表qty總和. 相當(dāng)于Sum(qty)性質(zhì).
          @@constName   @@User_ID要點(diǎn):獲取內(nèi)存變量值;

          實(shí)現(xiàn)如下:
          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
          主站蜘蛛池模板: 广昌县| 青川县| 洛扎县| 明星| 龙山县| 手游| 鸡泽县| 山西省| 临海市| 耒阳市| 惠安县| 金华市| 乌兰察布市| 喀喇沁旗| 连山| 胶南市| 都安| 桂平市| 霸州市| 尉犁县| 金寨县| 凤山市| 韶山市| 甘泉县| 乡宁县| 太湖县| 海南省| 正镶白旗| 从化市| 澄城县| 鄂托克前旗| 灌阳县| 江口县| 巩留县| 博野县| 视频| 花莲市| 会理县| 宜宾县| 乐平市| 阿荣旗|