Java正则表达式问题?

Java正则表达式问题?,java,html,regex,Java,Html,Regex,好的,我的老师给我布置了一个作业,让我读入两个apache web服务器日志,例如 10.10.10.10 - - [27/Sep/2016:05:22:00 +0000] "GET /1.1/friendships/list.json?user_id=123 HTTP/1.1" 500 563 19 "Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)" 177.177.177.177 10.10.10.10 - - [27/Sep/

好的,我的老师给我布置了一个作业,让我读入两个apache web服务器日志,例如

10.10.10.10 - - [27/Sep/2016:05:22:00 +0000] "GET /1.1/friendships/list.json?user_id=123 HTTP/1.1" 500 563 19 "Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)" 177.177.177.177

10.10.10.10 - - [27/Sep/2016:05:22:08 +0000] "GET /1.1/friendships/list.json?user_id=123 HTTP/1.1" 200 563 19 "Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)" 177.177.177.177

10.10.10.10 - - [27/Sep/2016:05:22:31 +0000] "GET /1.1/friendships/list.json HTTP/1.1" 200 563 19 "Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)" 177.177.177.177

10.10.10.10 - - [27/Sep/2016:05:22:59 +0000] "GET /1.1/friendships/list.json HTTP/1.1" 200 94 6 "Twitter-iPhone/6.63 iOS/10.0.1 (Apple;iPhone7,2;;;;;1)" 177.177.177.177

10.10.10.10 - - [27/Sep/2016:05:23:01 +0000] "GET /1.1/users/show.json?include_entities=1&user_id=321 HTTP/1.1" 200 4160 51 "Twitter-iPhone/6.63 iOS/9.3.5 (Apple;iPhone7,2;;;;;0)" 177.177.177.177

10.10.10.10 - - [27/Sep/2016:22:45:33 +0000] "GET /1.1/friendships/list.json?user_id=234 HTTP/1.1" 200 563 19 "Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)" 177.177.177.177

10.10.10.10 - - [27/Sep/2016:22:45:51 +0000] "POST /1.1/friendships/create.json HTTP/1.1" 200 4193 120 "Twitter-iPhone/6.62.1 iOS/9.3.5 (Apple;iPhone7,2;;;;;0)" 177.177.177.177
端点是URL的路径组件。 例如,
/1.1/friendships/create.json

成功率是给定端点的非500级响应代码对总请求的级别,表示为小数点后两个百分点的百分比

比如说

10.10.10.10 - - [27/Sep/2016:05:22:31 +0000] "GET /1.1/friendships/list.json HTTP/1.1" (response code is here)200 563 19 "Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)" 177.177.177.177
我的代码应该打印出以下内容,这些内容首先按时间排序,然后按字典顺序排序,如下所示

2016-09-27T05:22 /1.1/friendships/list.json 75.00

2016-09-27T05:23 /1.1/users/show.json 100.00

2016-09-27T22:45 /1.1/friendships/create.json 100.00

2016-09-27T22:45 /1.1/friendships/list.json 100.00
以下是我的尝试:

import java.io.*;
import static java.lang.String.format;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class JavaApplication17 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // String line = "10.10.10.10 - - [27/Sep/2016:05:22:00 +0000] \"GET /1.1/friendships/list.json?XXXXX HTTP/1.1\" 500 563 19 \"Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)\" 177.177.177.177";
        LL myLL = new LL();
        String responseCodeCopy = "";
        int responseCodeAsNumber;
        Pattern a = Pattern.compile("(\\[(?<time>.*)\\])?");
        Pattern responseCodeString = Pattern.compile("\"\\s+(?<resCode>[0-9]{0,3})\\s+");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String line1;
        String myString = null;
        String newLine;

        while (((line1 = bufferedReader.readLine()) != null) && (line1.length() != 0)) {
            myString = line1;
            try{
            String[] firstSplit = myString.split(" - - ");
            String rest1 = firstSplit[1];

            String time = null;
            Matcher m1 = a.matcher(rest1);
            if (m1.find()) {
                time = m1.group("time");
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm", Locale.ENGLISH);
                Date d1 = null;
                d1 = sdf.parse(time);
                String timeAsString = sdf.format(d1);
                long totalTime = d1.getTime();
                String[] endPointCopy = rest1.split("\"");
                String rest2 = endPointCopy[1];
                String[] splitByQuestionMark = rest2.split("\\?");
                String[] split3 = splitByQuestionMark[0].split(" ");
                String url = split3[1];
                Matcher responseCodeMatch = responseCodeString.matcher(myString);
                if (responseCodeMatch.find()) {
                    responseCodeCopy = responseCodeMatch.group("resCode").trim();
                }
                responseCodeAsNumber = Integer.parseInt(responseCodeCopy);
                newLine = timeAsString + " " + url;

                if (responseCodeAsNumber >= 500) {
                    myLL.sortedInsert(newLine, d1, url, false);
                } else {
                    myLL.sortedInsert(newLine, d1, url, true);
                }

            }

             }catch(Exception e){
                System.out.println(line1);
                e.printStackTrace();
            }

        }

        myLL.printList();

    }//end of main

}//end of main class

class LL {

    public Node head;

    public LL() {
        this.head = null;
    }

    public void sortedInsert(String text2, Date d, String text3, boolean isHit) {

        Node temp = this.head;
        Node prev = null;
        long totalTimeCopy = d.getTime();

        while ((temp != null) && (temp.compare(totalTimeCopy, text3) < 0)) {
            prev = temp;
            temp = temp.next;
        }

        if (temp != null) {

            if (prev != null) {
                if (temp.compare(totalTimeCopy, text3) == 0) {

                    if (isHit) {
                        temp.incrementHit();
                    } else {
                        temp.incrementMiss();
                    }

                } else {
                    Node nnode;
                    if (isHit) {
                        nnode = new Node(text2, d, 1, 0, null);
                    } else {
                        nnode = new Node(text2, d, 0, 1, null);
                    }
                    nnode.next = temp;
                    prev.next = nnode;

                }
            } else if (temp.compare(totalTimeCopy, text3) == 0) {

                if (isHit) {
                    temp.incrementHit();
                } else {
                    temp.incrementMiss();
                }

            } else {
                Node nnode;
                if (isHit) {
                    nnode = new Node(text2, d, 1, 0, null);
                } else {
                    nnode = new Node(text2, d, 0, 1, null);
                }

                this.head = nnode;
                nnode.next = temp;

            }

        } else {

            Node nnode;
            if (isHit) {
                nnode = new Node(text2, d, 1, 0, null);
            } else {
                nnode = new Node(text2, d, 0, 1, null);
            }
            if (prev == null) {
                this.head = nnode;
            } else {
                prev.next = nnode;
            }

        }

    }

    public void printList() throws FileNotFoundException {
        Node temp = this.head;

        while (temp != null) {

            int Hit = temp.getHit();
            int Miss = temp.getMiss();
            DecimalFormat f = new DecimalFormat("##.00");
            double sucRate = Hit * 100 / (Hit + Miss);

            System.out.println(temp.getTimetext() + " " + temp.getEndpoint() + " " + f.format(sucRate));
            temp = temp.next;
        }

    }

}// end of LL

class Node {

    private String text;
    private int Hit;
    private int Miss;
    public Node next;
    public Date date;

    public Node(String text, Date d, int Hit, int Miss, Node next) {
        this.text = text;
        this.date = d;
        this.Hit = Hit;
        this.Miss = Miss;
        this.next = next;
    }

    public String getText() {
        return this.text;
    }

    public String getEndpoint() {
       // will get url(endpoint)
        String[] splitEndPoint = this.text.split(" ");
        return splitEndPoint[1];
    }

    public long getTime() {
        return this.date.getTime();
    }

    public String getTimetext() {
        Date d = new Date(this.date.getTime());
        SimpleDateFormat print = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        return print.format(d);
        // return str
    }

    public int getHit() {
        return this.Hit;
    }

    public int getMiss() {
        return this.Miss;
    }

    public void incrementHit() {
        this.Hit = this.Hit + 1;
    }

    public void incrementMiss() {
        this.Miss = this.Miss + 1;
    }

    public int compare(long Time, String Text3) {
        // if other is greater then

        if (this.getTime() < Time) {
            return -1;
        }

        if (this.getTime() > Time) {
            return 1;
        }

        if (this.getTime() == Time) {

            if (this.getEndpoint().compareTo(Text3) > 0) {
                //will return positive if 
                return 1;
            }

            if (this.getEndpoint().compareTo(Text3) < 0) {
                return -1;
            }

            if (this.getEndpoint().compareTo(Text3) == 0) {
                return 0;
            }

        }

        return 0;

    }

}
import java.io.*;
导入静态java.lang.String.format;
导入java.util.*;
导入java.text.*;
导入java.math.*;
导入java.util.regex.*;
公共类JavaApplication17{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发异常{
//String line=“10.10.10.10---[27/Sep/2016:05:22:00+0000]\“GET/1.1/friendships/list.json?XXXXX HTTP/1.1\“500 563 19\”推特iPhone/6.63 iOS/10.0.2(苹果;iPhone 7,2;;1)\“177.177.177.177”;
LL myLL=新的LL();
字符串responseCodeCopy=“”;
国际反应理事会成员;
模式a=Pattern.compile(“(\\[(?*)\\])?”;
Pattern responseCodeString=Pattern.compile(“\”\\s+(?[0-9]{0,3})\\s+”);
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(System.in));
字符串行1;
字符串myString=null;
字符串换行符;
而((line1=bufferedReader.readLine())!=null)和&(line1.length()!=0)){
myString=line1;
试一试{
String[]firstSplit=myString.split(“-”);
字符串rest1=firstSplit[1];
字符串时间=空;
匹配器m1=a.匹配器(rest1);
if(m1.find()){
时间=m1.组(“时间”);
SimpleDataFormat sdf=新的SimpleDataFormat(“dd/MMM/yyyy:HH:mm”,Locale.ENGLISH);
日期d1=空;
d1=sdf.parse(时间);
字符串timeAsString=sdf.format(d1);
long totalTime=d1.getTime();
字符串[]endPointCopy=rest1.split(“\”);
字符串rest2=endPointCopy[1];
String[]splitByQuestionMark=rest2.split(“\\?”);
字符串[]split3=splitByQuestionMark[0]。拆分(“”);
字符串url=split3[1];
Matcher responseCodeMatch=responseCodeString.Matcher(myString);
if(responseDematch.find()){
responseCodeCopy=responseCodeMatch.group(“重新编码”).trim();
}
responsecodesnumber=Integer.parseInt(responseCodeCopy);
换行符=timeAsString+“”+url;
如果(响应数>=500){
myLL.sortedInsert(换行符,d1,url,false);
}否则{
myLL.sortedInsert(换行符,d1,url,true);
}
}
}捕获(例外e){
系统输出打印项次(第1行);
e、 printStackTrace();
}
}
myLL.printList();
}//干管末端
}//主课结束
LL班{
公共节点头;
公共法律责任(){
this.head=null;
}
public void sortedInsert(字符串text2,日期d,字符串text3,布尔值isHit){
节点温度=this.head;
Node prev=null;
long totalTimeCopy=d.getTime();
while((temp!=null)和&(temp.compare(totalTimeCopy,text3)<0)){
prev=温度;
温度=下一个温度;
}
如果(温度!=null){
如果(上一个!=null){
如果(温度比较(totalTimeCopy,text3)==0){
if(isHit){
温度增量hit();
}否则{
temp.incrementMiss();
}
}否则{
节点nnode;
if(isHit){
nnode=新节点(text2,d,1,0,null);
}否则{
nnode=新节点(text2,d,0,1,null);
}
nnode.next=温度;
prev.next=nnode;
}
}否则如果(温度比较(totalTimeCopy,text3)==0){
if(isHit){
温度增量hit();
}否则{
temp.incrementMiss();
}
}否则{
节点nnode;
if(isHit){
nnode=新节点(text2,d,1,0,null);
}否则{
nnode=新节点(text2,d,0,1,null);
}
this.head=nnode;
nnode.next=温度;
}
}否则{
节点nnode;
if(isHit){
nnode=新节点(text2,d,1,0,null);
}否则{
nnode=新节点(text2,d,0,1,null);
}
if(prev==null){
this.head=nnode;
}否则{
prev.next=nnode;
}
}
}
public void printList()引发FileNotFoundException{
节点温度=this.head;
while(temp!=null){
int Hit=temp.getHit();
int Miss=temp.getMiss();
DecimalFormat f=新的DecimalFormat(“##.00”);
双倍成功率=命中*100/(命中+未命中);
System.out.println(temp.getTimetext()+“”+temp.getEndpoint()+“”+f.format(sucRate));
温度=下一个温度;
}
}
}//末了
类节点{
私有字符串文本;
私人int Hit;
私人int Miss;
公共节点下一步;
公开日期;
公共节点(字符串文本、日期d、整数命中、整数未命中、节点下一个){
this.text=文本;
这个日期=d;
Hit=Hit;
这个。小姐=小姐;
this.next=next;
}
Pattern full = Pattern.compile("^(\\S+\\s+){3}\\[(?<time>[^]]*)\\]\\s+\"[^\"]*\"\\s+(?<resCode>[0-9]*)\\s");
^(\S+\s+){3}\[(?<time>[^]]*)\]\s+"[^"]*"\s+(?<resCode>[0-9]*)\s
  ~~~~~~                                                        matches a string followed by whitespace
         ~~~                                                    does so three times
              ~~~~~~~~~~~~~~                                    matches anything between [...] that DOES NOT CONTAIN a ]
                              ~~~                               matches whitespace
                                 ~~~~~~~                        matches a string between precisely one pair of "..."
                                           ~~~~~~~~~~~~~~~~~~   your resCode follows, delimites by whitespace on both sides
double sucRate = Hit * 100 / (Hit + Miss);
double sucRate = Hit * 100.0 / (Hit + Miss);
double sucRate = Hit * 100.0 / (double)(Hit + Miss);
double sucRate = (double)(Hit * 100 / (Hit + Miss));