Readline 和正则表达式匹配出现问题

发布于 2024-12-24 15:44:58 字数 1076 浏览 4 评论 0原文

我的计算机上有一个(ANSI)文本文件(filters.txt),每一行都是一个正则表达式。

在我的 main 中,我循环浏览 RSS feed;每个项目都会传递给另一个方法来检查filters.txt 中的匹配项。

问题是似乎没有什么是匹配的。

  • 我传递给该方法的字符串是正确的(已测试)
  • 我的正则表达式是正确的(已测试)
  • 来自filters.txt的输入“似乎”与文本文件中的相同
  • 也许换行符或其他东西正在搞乱模式匹配?我是否需要使用其他工具来读取文本文件才能完成此任务?

这是我的方法的代码:

public static boolean filtermatch(String rsstitle) {
    boolean result = false;

      try {
          BufferedReader br = new BufferedReader(new FileReader("filters.txt"));
          String strLine;

          while ((strLine = br.readLine())!= null && result == false) {
              if(Pattern.matches("(?i)" + strLine, rsstitle)) {
                  result = true;
              }
          }   
          br.close();
      } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
      }

    return result;
}

编辑: 我现在只有一个过滤器:

\\[HorribleSubs\\].*Chihayafuru.*

应该匹配“[HorribleSubs] Chihayafuru - 12 [720p].mkv”,但事实并非如此。如果我在程序中定义正则表达式,它会匹配它。

I have a (ANSI) text file (filters.txt) on my computer, on each line is a regex.

In my main, I loop through an RSS feed; each item is passed to another method to check for matches in filters.txt.

The problem is nothing seems to be matching.

  • The string I'm passing to the method is correct (tested)
  • My regexps are correct (tested)
  • Input from filters.txt "seems" to be the same as in the text file
  • Perhaps a line break or something else is screwing up the pattern matches? Do i need to use some other tool to read the text files to get this done?

Here's the code of my method:

public static boolean filtermatch(String rsstitle) {
    boolean result = false;

      try {
          BufferedReader br = new BufferedReader(new FileReader("filters.txt"));
          String strLine;

          while ((strLine = br.readLine())!= null && result == false) {
              if(Pattern.matches("(?i)" + strLine, rsstitle)) {
                  result = true;
              }
          }   
          br.close();
      } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
      }

    return result;
}

EDIT:
Only filter i have on right now:

\\[HorribleSubs\\].*Chihayafuru.*

Should be matching "[HorribleSubs] Chihayafuru - 12 [720p].mkv" yet it's not. It matches it if I define the regex in the program though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浸婚纱 2024-12-31 15:44:58

不要在文件中放置双反斜杠:Java 会将它们转换为“\\\\”。它读取单个反斜杠,如“\\”。

Don't put double backslashes in the file: Java will transform them in "\\\\". It reads a single backslash like "\\".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文