使用正则表达式解析

时间:2013-07-09 10:54:23

标签: java regex

我有一个像

这样的字符串
DELIVERY 'AR_ACTDETAIL_UPD' 'AR_DETAIL_UPD'
MODULE TABLE
FILTER 'AR_ACT'
DEFINE UPDN 'UPDATE'
DEFINE REFH 'UPDATE'
DEFINE CIT '5000'
DEFINE TDB 'TARGET'
DEFINE TABLE 'AR_ACTIVITY_DETAIL'
ELEMENTS '"POSTING_PERIOD_SID","POSTING_DATE_SID","ACCTG_DOC_CLSS_SID","CUSTOMER_BLLT_SID","AR_ASSET_ACCNT_SID","CO_CNSLDTN_SID","AR_BSNSS_UNT_SID","LOCAL_CURRENCY_AMT","LCL_CUR_DSCNT_AAMT","LCL_CUR_DSCNT_TAMMT","TAXABLE_AMT","NO_TAXABLE_AMT","NO_FOREIGN_TAXABLE_AMT","TAX_CODE","BEFOREINVOICE_SID","ACCNT_BANK_PAYMENT_SID","GAIN_LOSS_LOC_AMT","LOCAL_OPEN_AMOUNT","FOREIGN_OPEN_AMOUNT"'
ELEDEFINE +3 IFNULL '0'
ELEDEFINE +4 IFNULL '0'
ELEDEFINE +5 IFNULL '0'
ELEDEFINE +6 IFNULL '0'

DELIVERY 'AR_DOC_EXINS' 'AR_DOC_EINS'
MODULE TABLE
FILTER 'AR_DOC_EXTNSN_COLS_INS'
DEFINE UPDATE_DETECTION 'UPDATE'
DEFINE REFRESH 'APPEND'
DEFINE COMMIT '5000'
DEFINE TABLEDB 'TARGET'
DEFINE TABLE 'AR_DOC_EXTNSN_COLS'
ELEMENTS '"AR_ACTVSID","DOCUMENT_CO_CD","TRANSACTION_TYP_CD","TRANSACTION_NO","DOCUMENT_SUFFIX_NO","DOC_SFFX_EXTT_NO","INVOICE_MULT","PAYMENT_MULT","ADJUSTMENT_MULT","BAD_DEBT_MULT","SOURCE_INFO","CHG_INVC_PY_STTS_IND","CHG_GROUP_CURRENCY_AMT","CHG_GRP_CUR_DSCNT_AAMT","CHG_GRP_CUR_DSCNT_TAMT","CREATED_DT","CHANGED_DT","ACCOUNT_ID","FI_ACCOUNT_ID"'
ELEDEFINE +2 IFNULL '{$U_DEFAULT_SHORT_CHAR}'
ELEDEFINE +3 IFNULL '{$U_DEFAULT_SHORT_CHAR}'

DELIVERY 'AR_DOC_EXS_UPD' 'AR_DOC__UPD'
MODULE TABLE
FILTER 'AR_DOC_EXTNSN_COLS_UPD'
DEFINE UPDATE_DETECTION 'UPDATE'
DEFINE REFRESH 'UPDATE'
DEFINE COMMIT '5000'
DEFINE TABLEDB 'TARGET'
DEFINE TABLE 'AR_DOC_EOLS'
ELEMENTS '"DOCUMENT_CO_CD","TRANSACTION_TYP_CD","TRANSACTION_NO","DOCUMENT_SUFFIX_NO","DOC_SFFX_EXTNSN_NO","PAYMENT_ID","PAYMETED_DT","CHANGED_DT","AR_ACTVTY_DOC_SID_TMP","ACCOUNT_ID","FI_ACCOUNT_ID","O_GROUP_CURRENCY_AMT","O_GRP_CUR_DSCNT_TAMT","O_GRP_CUR_DSCNT_AAMT"'
ELEDEFINE +1 IFNULL '{$U_DEFAULT_SHORT_CHAR}'
ELEDEFINE +2 IFNULL '{$U_DEFAULT_SHORT_CHAR}'
ELEDEFINE +3 IFNULL '{$U_DEFAULT_NUMBER}'

ALLOW DUPLICATES

发货次数未知,但如果我发现ALLOWREJECTMERGEAGGREGATE,则表示没有更多的发货。我的想法是分开提取每个DELIVERY源代码(这是在交付和其中一个单词之间存在的两个交付或子串之间存在的子串(ALLOW,REJECT,MERGE,AGGREGATE))所以尝试了这个模式:

Pattern p = Pattern.compile("DELIVERY\\s.*?[\\nDELIVERY|\\nALLOW|\\nREJECT|\\nMERGE|\\nAGGREGATE]?",Pattern.DOTALL);

但它对我不起作用。

2 个答案:

答案 0 :(得分:3)

描述

此正则表达式将捕获示例文本中的三个交付中的每一个

^delivery(?:'[^']*'|.)*?^(?=delivery|aggregate|reject|allow|merge)

enter image description here

实施例

DELIVERY 'AR_ACTDETAIL_UPD' 'AR_DETAIL_UPD'
MODULE TABLE
FILTER 'AR_ACT'
DEFINE UPDN 'UPDATE'
DEFINE REFH 'UPDATE'
DEFINE CIT '5000'
DEFINE TDB 'TARGET'
DEFINE TABLE 'AR_ACTIVITY_DETAIL'
ELEMENTS '"POSTING_PERIOD_SID","POSTING_DATE_SID","ACCTG_DOC_CLSS_SID","CUSTOMER_BLLT_SID","AR_ASSET_ACCNT_SID","CO_CNSLDTN_SID","AR_BSNSS_UNT_SID","LOCAL_CURRENCY_AMT","LCL_CUR_DSCNT_AAMT","LCL_CUR_DSCNT_TAMMT","TAXABLE_AMT","NO_TAXABLE_AMT","NO_FOREIGN_TAXABLE_AMT","TAX_CODE","BEFOREINVOICE_SID","ACCNT_BANK_PAYMENT_SID","GAIN_LOSS_LOC_AMT","LOCAL_OPEN_AMOUNT","FOREIGN_OPEN_AMOUNT"'
ELEDEFINE +3 IFNULL '0'
ELEDEFINE +4 IFNULL '0'
ELEDEFINE +5 IFNULL '0'
ELEDEFINE +6 IFNULL '0'

DELIVERY 'AR_DOC_EXINS' 'AR_DOC_EINS'
MODULE TABLE
FILTER 'AR_DOC_EXTNSN_COLS_INS'
DEFINE UPDATE_DETECTION 'UPDATE'
DEFINE REFRESH 'APPEND'
DEFINE COMMIT '5000'
DEFINE TABLEDB 'TARGET'
DEFINE TABLE 'AR_DOC_EXTNSN_COLS'
ELEMENTS '"AR_ACTVSID","DOCUMENT_CO_CD","TRANSACTION_TYP_CD","TRANSACTION_NO","DOCUMENT_SUFFIX_NO","DOC_SFFX_EXTT_NO","INVOICE_MULT","PAYMENT_MULT","ADJUSTMENT_MULT","BAD_DEBT_MULT","SOURCE_INFO","CHG_INVC_PY_STTS_IND","CHG_GROUP_CURRENCY_AMT","CHG_GRP_CUR_DSCNT_AAMT","CHG_GRP_CUR_DSCNT_TAMT","CREATED_DT","CHANGED_DT","ACCOUNT_ID","FI_ACCOUNT_ID"'
ELEDEFINE +2 IFNULL '{$U_DEFAULT_SHORT_CHAR}'
ELEDEFINE +3 IFNULL '{$U_DEFAULT_SHORT_CHAR}'

DELIVERY 'AR_DOC_EXS_UPD' 'AR_DOC__UPD'
MODULE TABLE
FILTER 'AR_DOC_EXTNSN_COLS_UPD'
DEFINE UPDATE_DETECTION 'UPDATE'
DEFINE REFRESH 'UPDATE'
DEFINE COMMIT '5000'
DEFINE TABLEDB 'TARGET'
DEFINE TABLE 'AR_DOC_EOLS'
ELEMENTS '"DOCUMENT_CO_CD","TRANSACTION_TYP_CD","TRANSACTION_NO","DOCUMENT_SUFFIX_NO","DOC_SFFX_EXTNSN_NO","PAYMENT_ID","PAYMETED_DT","CHANGED_DT","AR_ACTVTY_DOC_SID_TMP","ACCOUNT_ID","FI_ACCOUNT_ID","O_GROUP_CURRENCY_AMT","O_GRP_CUR_DSCNT_TAMT","O_GRP_CUR_DSCNT_AAMT"'
ELEDEFINE +1 IFNULL '{$U_DEFAULT_SHORT_CHAR}'
ELEDEFINE +2 IFNULL '{$U_DEFAULT_SHORT_CHAR}'
ELEDEFINE +3 IFNULL '{$U_DEFAULT_NUMBER}'

ALLOW DUPLICATES

<强>代码

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
  public static void main(String[] asd){
  String sourcestring = "source string to match with pattern";
  Pattern re = Pattern.compile("^delivery(?:'[^']*'|.)*?^(?=delivery|aggregate|reject|allow|merge)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
  Matcher m = re.matcher(sourcestring);
  int mIdx = 0;
    while (m.find()){
      for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
        System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
      }
      mIdx++;
    }
  }
}

<强>匹配

[0][0] = DELIVERY 'AR_ACTDETAIL_UPD' 'AR_DETAIL_UPD'
MODULE TABLE
FILTER 'AR_ACT'
DEFINE UPDN 'UPDATE'
DEFINE REFH 'UPDATE'
DEFINE CIT '5000'
DEFINE TDB 'TARGET'
DEFINE TABLE 'AR_ACTIVITY_DETAIL'
ELEMENTS '"POSTING_PERIOD_SID","POSTING_DATE_SID","ACCTG_DOC_CLSS_SID","CUSTOMER_BLLT_SID","AR_ASSET_ACCNT_SID","CO_CNSLDTN_SID","AR_BSNSS_UNT_SID","LOCAL_CURRENCY_AMT","LCL_CUR_DSCNT_AAMT","LCL_CUR_DSCNT_TAMMT","TAXABLE_AMT","NO_TAXABLE_AMT","NO_FOREIGN_TAXABLE_AMT","TAX_CODE","BEFOREINVOICE_SID","ACCNT_BANK_PAYMENT_SID","GAIN_LOSS_LOC_AMT","LOCAL_OPEN_AMOUNT","FOREIGN_OPEN_AMOUNT"'
ELEDEFINE +3 IFNULL '0'
ELEDEFINE +4 IFNULL '0'
ELEDEFINE +5 IFNULL '0'
ELEDEFINE +6 IFNULL '0'


[1][0] = DELIVERY 'AR_DOC_EXINS' 'AR_DOC_EINS'
MODULE TABLE
FILTER 'AR_DOC_EXTNSN_COLS_INS'
DEFINE UPDATE_DETECTION 'UPDATE'
DEFINE REFRESH 'APPEND'
DEFINE COMMIT '5000'
DEFINE TABLEDB 'TARGET'
DEFINE TABLE 'AR_DOC_EXTNSN_COLS'
ELEMENTS '"AR_ACTVSID","DOCUMENT_CO_CD","TRANSACTION_TYP_CD","TRANSACTION_NO","DOCUMENT_SUFFIX_NO","DOC_SFFX_EXTT_NO","INVOICE_MULT","PAYMENT_MULT","ADJUSTMENT_MULT","BAD_DEBT_MULT","SOURCE_INFO","CHG_INVC_PY_STTS_IND","CHG_GROUP_CURRENCY_AMT","CHG_GRP_CUR_DSCNT_AAMT","CHG_GRP_CUR_DSCNT_TAMT","CREATED_DT","CHANGED_DT","ACCOUNT_ID","FI_ACCOUNT_ID"'
ELEDEFINE +2 IFNULL '{$U_DEFAULT_SHORT_CHAR}'
ELEDEFINE +3 IFNULL '{$U_DEFAULT_SHORT_CHAR}'


[2][0] = DELIVERY 'AR_DOC_EXS_UPD' 'AR_DOC__UPD'
MODULE TABLE
FILTER 'AR_DOC_EXTNSN_COLS_UPD'
DEFINE UPDATE_DETECTION 'UPDATE'
DEFINE REFRESH 'UPDATE'
DEFINE COMMIT '5000'
DEFINE TABLEDB 'TARGET'
DEFINE TABLE 'AR_DOC_EOLS'
ELEMENTS '"DOCUMENT_CO_CD","TRANSACTION_TYP_CD","TRANSACTION_NO","DOCUMENT_SUFFIX_NO","DOC_SFFX_EXTNSN_NO","PAYMENT_ID","PAYMETED_DT","CHANGED_DT","AR_ACTVTY_DOC_SID_TMP","ACCOUNT_ID","FI_ACCOUNT_ID","O_GROUP_CURRENCY_AMT","O_GRP_CUR_DSCNT_TAMT","O_GRP_CUR_DSCNT_AAMT"'
ELEDEFINE +1 IFNULL '{$U_DEFAULT_SHORT_CHAR}'
ELEDEFINE +2 IFNULL '{$U_DEFAULT_SHORT_CHAR}'
ELEDEFINE +3 IFNULL '{$U_DEFAULT_NUMBER}'

答案 1 :(得分:2)

以下代码应该适合您:

Pattern p = Pattern.compile("(?s)(\\bDELIVERY\\b.+?(?=\\b(?:DELIVERY|ALLOW|REJECT|MERGE|AGGREGATE)\\b))");
Matcher m = p.matcher(str); // str is your input string
boolean found = false;
while (m.find()) {
    System.out.printf("Deliver: %s%n", m.group(1));
    found = true;
}

if (!found) {
    System.out.println("Didn't Match");
}