【问题标题】:Multiple line search in a file using java or unix command [closed]使用java或unix命令在文件中进行多行搜索[关闭]
【发布时间】:2015-08-31 17:12:42
【问题描述】:

我想在像

这样的文件中找到以下模式
subclass "Pool1" 11:22:33:44:55:66    {

      dynamic;

}

我必须在文件中找到上述模式。

如何使用 java 或 Unix 命令找到?

【问题讨论】:

  • 你要解析哪一部分?你需要怎样的结果?
  • 我在 dhcpd.leases 文件中提到的子类 "Pool1" 11:22:33:44:55:66 { dynamic } 的模式

标签: java shell unix command


【解决方案1】:

你可以找到这样的。

    File file = new File("data/pattern.txt");
    Pattern pat = Pattern.compile("subclass \"Pool1\" 11:22:33:44:55:66 \\{\\s*dynamic;\\s*\\}");
    String content = Files.lines(file.toPath()).collect(Collectors.joining("\n"));
    Matcher m = pat.matcher(content);
    while (m.find()) {
        System.out.printf("found at %d-%d%n", m.start(), m.end());
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2014-03-10
    相关资源
    最近更新 更多