Readline 和正则表达式匹配出现问题
我的计算机上有一个(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在文件中放置双反斜杠:Java 会将它们转换为“\\\\”。它读取单个反斜杠,如“\\”。
Don't put double backslashes in the file: Java will transform them in "\\\\". It reads a single backslash like "\\".