1

编辑问题我想从一些字符串中提取日期和时间。这是一个例子。所有事件字符串都以 [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

我想将日期和时间提取为每个活动的一个变量。例如

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

我尝试了以下

/*
String sample = "[0r(1)2[000p[040qe1w3h162[020t*882*11/11/2010*12:26*";
String regex = "(?x) ^([0r(1)2[000p[040qe1w3h162[020t*):// ([^/:]+) (?:(\\d+))?";
Matcher m = Pattern.compile(regex).matcher(sample);
if(m.find())
{
String ignore = m.group();
String date = m.group(1); 
String time = m.group(2);
System.out.println( date + "     " +  time);
}
*/
//this section isn't useful in light of the edit to the question
4

2 回答 2

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);
        }


    }
}
于 2013-09-05T08:47:57.323 回答
0

使用String.split(String regex)

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);

输出:

date=11/11/2010, time=12:26
于 2013-09-03T07:44:33.107 回答