Java 解析日志文件以提取查询

Java 解析日志文件以提取查询,java,regex,logging,Java,Regex,Logging,我想从日志文件中提取特定的URL。但我只想提取那些排名1.或2的查询。日志文件包含一个columitemrank,给出等级。 到目前为止,我能够通过扫描文本来提取特定的URL。但我不知道如何实现只在第一次或第二次单击URL的条件 例如,这是日志文件的一部分的外观: (列为ID、日期、时间、排名、url) 7635702006-03-0614:09:482 7635702006-03-0614:09:483 这里我只想提取第一个查询,因为它排名第二 这是我目前的代码: public class

我想从日志文件中提取特定的URL。但我只想提取那些排名1.2的查询。日志文件包含一个columitemrank,给出等级。 到目前为止,我能够通过扫描文本来提取特定的URL。但我不知道如何实现只在第一次或第二次单击URL的条件

例如,这是日志文件的一部分的外观:

(列为ID、日期、时间、排名、url)

7635702006-03-0614:09:482

7635702006-03-0614:09:483

这里我只想提取第一个查询,因为它排名第二


这是我目前的代码:

public class Scanner {

    public static void main(String[] args) throws FileNotFoundException {


        File testFile = new File ("C:/Users/Zyaad/logs.txt");
        Scanner s = new Scanner(testFile);
        int count=0;

        String pattern="http://ontology.buffalo.edu";
        while(s.hasNextLine()){
            String line = s.nextLine();

            if (line.contains(pattern)){
                count++;

                System.out.println(count + ".query: " );
                System.out.println(line);
            } 

        }   System.out.println("url was clicked: "+ count + " times");

        s.close();

        }
}       
我怎样才能打印出1。查询我尝试了类似regex的
[\t\n\b\r\f][1,2]{1}[\t\n\b\r\f]
,但没有成功。

一个简单(可能过于简单)的方法是:

  • 确定您要查找的数量(严重程度?)
  • 确定URL的起始模式
  • 示例

    // assume this is the file you're parsing so I don't have to repeat 
    // the whole Scanner part here
    String theFile = "763570 2006-03-06 14:09:48 2 http://something2.com\r\n" +
            "763570 2006-03-06 14:09:48 3 http://something3.com";
    //                           | your starting digit of choice
    //                           | | one white space
    //                           | | | group 1 start
    //                           | | | | partial protocol of the URL
    //                           | | | |  | any character following in 1+ instances
    //                           | | | |  | | end of group 1
    //                           | | | |  | | 
    Pattern p = Pattern.compile("2\\s(http.+)");
    Matcher m = p.matcher(theFile);
    while (m.find()) {
        // back-referencing group 1
        System.out.println(m.group(1));
    }
    
    输出

    http://something2.com
    
    注意

    // assume this is the file you're parsing so I don't have to repeat 
    // the whole Scanner part here
    String theFile = "763570 2006-03-06 14:09:48 2 http://something2.com\r\n" +
            "763570 2006-03-06 14:09:48 3 http://something3.com";
    //                           | your starting digit of choice
    //                           | | one white space
    //                           | | | group 1 start
    //                           | | | | partial protocol of the URL
    //                           | | | |  | any character following in 1+ instances
    //                           | | | |  | | end of group 1
    //                           | | | |  | | 
    Pattern p = Pattern.compile("2\\s(http.+)");
    Matcher m = p.matcher(theFile);
    while (m.find()) {
        // back-referencing group 1
        System.out.println(m.group(1));
    }
    
    通常建议不要使用正则表达式解析日志文件


    您最好长期实现自己的解析器,并将标记逐项列为对象的属性(我假设每行1个),然后根据需要操作它们

    您可以基于日期和时间模式创建正则表达式,也可以简单地从时间模式开始

    yyyy-MM-dd hh:mm:ss 1|2 
    
    日期和时间模式后跟1或2

    \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s[1|2]\s
    
    \d{2}:\d{2}:\d{2}\s[1|2]\s
    
    时间模式后跟1或2

    \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s[1|2]\s
    
    \d{2}:\d{2}:\d{2}\s[1|2]\s
    

    示例代码:

    String[] str=new String[] { "763570 2006-03-06 14:09:48 2 http://something.com",
            "763570 2006-03-06 14:09:48 3 http://something.com" };
    
    Pattern p = Pattern
              .compile("\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\s[1|2]\\s");
    for (String s : str) {
        Matcher m = p.matcher(s);
        if (m.find()) {
            System.out.println(s.substring(m.end()));
        }
    } 
    
    你可以在这里找到一些。
    如果可以使用其他工具,我建议您使用一个令人印象深刻的工具来收集和解析日志。

    您可以提取排名1或2的URL,如下所示:

    /(?<=\s(?:1|2)\s).*$/
    
    /(?试试这个:

    public static void main(String[] args) throws FileNotFoundException {
    
        int count = 0;
        // create date pattern
        // source:https://github.com/elasticsearch/logstash/blob/master/patterns/grok-patterns
        String yearPattern = "(?>\\d\\d){1,2}";
        String monthNumPattern = "(?:0?[1-9]|1[0-2])";
        String monthDayPattern = "(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9])";
        String hourPattern = "(?:2[0123]|[01]?[0-9])";
        String minutePattern = "(?:[0-5][0-9])";
        String secondPattern = "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)";
        String datePattern = String.format("%s-%s-%s %s:%s:%s", yearPattern,
                monthNumPattern, monthDayPattern, hourPattern, minutePattern,
                secondPattern);
    
        // create url pattern
        // source: http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149
        String urlPattern = "(https?://)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([/\\w \\.-]*)*/?";
        Pattern pattern = Pattern.compile("(\\d+) (" + datePattern
                + ") (\\d+) (" + urlPattern + ")");
        String data = "763570 2006-03-06 14:09:48 3 http://something.com\n"
                + "763570 2006-03-06 14:09:48 2 http://something.com\n"
                + "763570 2006-03-06 14:09:48 1 http://something.com";
        ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
        java.util.Scanner s = new java.util.Scanner(is);
        while (s.hasNextLine()) {
            String line = s.nextLine();
            Matcher matcher = pattern.matcher(line);
            if (matcher.matches()) {
                if (matcher.find(3)) {
                    int rank = Integer.parseInt(matcher.group(3));
                    if (rank == 1 || rank == 2) {
                        count++;
                    }
                }
            }
        }
        System.out.println("url was clicked: " + count + " times");
    
        s.close();
    
    }
    
    这将为包含以下内容的文件输出“url被单击:2次”:

    • 7635702006-03-0614:09:483
    • 7635702006-03-0614:09:482
    • 7635702006-03-0614:09:48 1

    问题是否已解决,或者您正在寻找其他解决方案?尚未解决。问题是matcher requiers字符串,但我想搜索日志文件。处理该问题时,请按您已经处理的方式逐行读取日志文件,并使用下面回答的任何代码。谢谢!我尝试了您的建议。但是Match如何处理我的日志文件?不是吗cher需要一个字符序列吗?当我放入测试文件时,它不需要work@user3654158不客气。正如您所说,
    Matcher
    初始化需要一个
    CharSequence
    。根据您的使用情况,您可以将
    Matcher
    用于文本文件的每一行。否则,您可以将文本文件的所有行放入一个
    字符串中(例如,通过
    StringBuilder
    ),然后对该
    字符串使用
    匹配器。