正则表达式进行解析

时间:2013-09-03 14:29:01

标签: java regex parsing string-parsing

我想从事件日志中提取一些字符串中的数据。所有事件字符串都以[0r(1)2 [000p [040qe1w3h162 [020t *]开头。遇到一个新的,它应解析最后一个字符串集并获取一些数据。显示了一个示例事件。除了另一个事件的开始之外,还有几个事件将跟随自己,没有分隔符。

[0r(1)2[000p[040qe1w3h162[020t*881*11/11/2010*12:24*
 *EVENT STARTED*
[020t 12:24:06 SMARTCARD ENTERED
11\11\10     12:24     10390011
123456789098765432   6598
INVALID TRANSACTION, PLEASE CONTACT
ADMIN FOR ADVICE
-----------------------------------
[020t 12:24:52 FILE STACKED
[020t 12:24:59 FILE PRESENTED 0,5,0,0
[020t 12:25:03 FILE TAKEN
11\11\10     12:25     10390011
123456789098765432   6599
WITHDRAW          FILES10.00
[000p[040q(1     *6599*1*E*000050000,M-00,R-10200
-----------------------------------
[020t 12:25:34 SMARTCARD TAKEN
[020t 12:25:38 EVENT ENDED

我想提取日期和时间作为每个活动的一个变量。 e.g。

Activity= EVENT STARTED
Activity time/date= 11/11/2010 12:24
Activity= SmartCard inserted
Activity time/date= 12:24:06

我想我已经编写了代码来识别带有下载代码的事件的开始。

import java.util.regex.*;
public class Test 
{

    public static void main(String args[])
    {
        String line = "[0r(1)2[000p[040qe1w3h162[020t*882*11/11/2010*12:26*";        
        String[] parts = line.split("\\*");
        String date = parts[2];
        String time = parts[3];
        System.out.println("date=" + date + ", time=" + time);
    }
}

谢谢

1 个答案:

答案 0 :(得分:0)

class sql
{
   public static void main (String [] args)
   {
        String dateInCase = "11/11/2010";
        String termID;
        String line = "    11\11\10     12:24     10390011";        
        String[] parts = line.split("");
        String termId = parts[4]+parts[5]+parts[6]; //to catch terminal ID
        String cardInserted = parts[1]+parts[2]+parts[3]+parts[4]+parts[5];
        String starter = parts[4]+parts[7]+parts[13]+parts[14]+parts[15];
        String tracker = parts[3]+parts[4]+parts[5]+parts[6]+parts[7];
        boolean V = (termId.matches("\\s\\d\\d"));
        boolean W = (cardInserted.matches("\\s\\s\\s\\s\\s"));//this gets card inserted
        boolean X = (starter.matches("\\D\\d\\d\\d\\d"));// a new event set has started 
        boolean Y = (tracker.matches("\\d\\d\\d\\D\\s")); // this checks for any activity as all activities have numbers in 3,4,5
        System.out.println(V);
        System.out.println(W);
        System.out.println(X);
        System.out.println(Y);

        if(V == true)
        {
            parts = line.split("\\     ");
            String terminal = parts[2];
            System.out.println("terminal " + terminal);
        }

        if(W == true)//this gets card inserted strings
        {
            parts =line.split("\\*");
            String activity = parts[1];
            System.out.print(activity);
        }

        if(X == true) //action if it sees a new event set
        {
            parts = line.split("\\*");
            String datetime = parts[2]+" "+ parts[3];
            System.out.println("time= " + datetime);
            dateInCase = parts[2];
        }

        if(Y == true) //action if it sees a new event
        {
            parts =line.split("\\ ");
            String datetime = dateInCase+ " " + parts[1];
            String activity = parts[2]+ " " + parts[3];
            System.out.println("time= " + datetime + " activity= " + activity);
        }


    }
}