【问题标题】:Need help to read specific line in a log which ends with specific word需要帮助来阅读日志中以特定单词结尾的特定行
【发布时间】:2018-04-06 05:31:34
【问题描述】:

我需要有关 java 代码的帮助来读取一个日志文件,该文件可以打印所有以 START 字结尾的行。

我的文件包含:

test 1 START
test2  XYZ
test 3 ABC
test 2 START

应该打印出来

test 1 START
test 2 START

我尝试了下面的代码,但它只打印 START。

 public class Findlog{
    public static void main(String[] args) throws IOException {

    BufferedReader r = new BufferedReader(new FileReader("myfile"));
    Pattern patt = Pattern.compile(".*START$");
            // For each line of input, try matching in it.
            String line;
            while ((line = r.readLine()) != null) {
              // For each match in the line, extract and print it.
              Matcher m = patt.matcher(line);
              while (m.find()) {
                // Simplest method:
                // System.out.println(m.group(0));

                // Get the starting position of the text

                System.out.println(line.substring(line));
              }
            }

【问题讨论】:

  • String 有一个名为endsWith 的方法。为什么不使用它而不是正则表达式?除此之外,您当前的问题很简单,不是打印您在其中读取的line,而是打印您的正则表达式匹配的行的一部分,即“START”。
  • 非常感谢 OH GOD SPIDERS ,我使用了 line 并删除了子字符串,它按预期工作。

标签: java file file-read


【解决方案1】:

line.endsWith("START") 检查已经足够了。这里不需要正则表达式。

【讨论】:

    【解决方案2】:

    我认为您已经找到了解决方案。 无论如何,应该工作的正则表达式是:

    ".*START$"
    

    它说:采取everithing(。*),然后是START,START是行尾($)

    【讨论】:

      【解决方案3】:

      您的代码的完整版本应如下所示

           public class Findlog{
              public static void main(String[] args) throws IOException {
      
              BufferedReader r = new BufferedReader(new FileReader("myfile"));
              String line;
              while ((line = r.readLine()) != null) {
                // For each match in the line, extract and print it.
                if(line.endsWith("START"))
                {
                   System.out.println(line);
                }
              }
      

      如果你想跳过区分大小写,那么代码应该如下所示。

      public class Findlog{
              public static void main(String[] args) throws IOException {
      
              BufferedReader r = new BufferedReader(new FileReader("myfile"));
              String line;
              while ((line = r.readLine()) != null) {
                // For each match in the line, extract and print it.
                if(line.toLowerCase().endsWith(matches.toLowerCase()))
                {
                   System.out.println(line);
                }
              }
      

      只需更改 if 条件

      【讨论】:

        【解决方案4】:
        public class Findlog{
            public static void main(String[] args) throws IOException {
        
            BufferedReader r = new BufferedReader(new FileReader("myfile"));
            Pattern patt = Pattern.compile(".*START$");
                    // For each line of input, try matching in it.
                    String line;
                    while ((line = r.readLine()) != null) {
                      // For each match in the line, extract and print it.
                      Matcher m = patt.matcher(line);
                      while (m.find()) {
                        // Simplest method:
                        // System.out.println(m.group(0));
        
                        // Get the starting position of the text
        
                        System.out.println(line.substring(line));
                      }
                    }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-11-28
          • 1970-01-01
          • 2020-02-13
          • 1970-01-01
          • 2018-01-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多