[英]Pattern.CASE_INSENSITIVE
I am working on a method that accepts a String argument parameter, searches a file for that word and returns a count of the occurrences of that word.我正在研究一种方法,该方法接受 String 参数,在文件中搜索该词并返回该词出现的次数。 I am using Java Regex Pattern and Matcher classes and methods.我正在使用 Java Regex Pattern 和 Matcher 类和方法。 The way that I have implemented Pattern.CASE_INSENSITIVE does not seem to function correctly.我实现 Pattern.CASE_INSENSITIVE 的方式似乎无法正常工作。 It is still matching on a case sensitive basis.它仍然在区分大小写的基础上匹配。
public int lookup(String wrd) throws IOException, FileNotFoundException,
{
int cnt = 0;
BufferedReader in = new BufferedReader(new FileReader(this.filename));
String line = in.readLine();
while (line != null)
{
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.mtch(line);
if (mtch.find()) {cnt++;}
line = input.readLine();
}
return cnt;
}
You are not scrolling through the lines in the file and only reading first line.您不是滚动文件中的行,而是只阅读第一行。 There is no reason to use regex, for most languages you can combine toLowerCase()
with indexOf()
to get a case insensitive position check.没有理由使用正则表达式,对于大多数语言,您可以将toLowerCase()
与indexOf()
结合使用以获得不区分大小写的位置检查。
public static void main(String[] args) throws Exception {
Path p = ...
String wrd = ...
System.out.println(totalMatches(p, wrd));
}
private static int totalMatches(Path path, String word) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(path)) {
return reader.lines()
.mapToInt(l -> lineMatches(l, word))
.sum();
}
}
private static int lineMatches(String line, String word) {
int counter = 0, i = 0, found;
while ((found = line.toLowerCase().indexOf(word.toLowerCase(), i)) >= 0) {
counter++;
i = found + 1;
}
return counter;
}
Just like I mentioned in the comment fix the mistakes.就像我在评论中提到的修复错误一样。 Here is a bit modified and fixed version of your code.这是您的代码的一些修改和固定版本。 This way seems like everything works:这种方式似乎一切正常:
public static int lookup(String wrd) throws IOException {
int cnt = 0;
BufferedReader in = new BufferedReader(
new StringReader(new String("word adfasdword avcasf\n asdf WoRd asdfWORDasdf")));
String line = in.readLine();
while (line != null) {
Pattern ptn = Pattern.compile(wrd, Pattern.CASE_INSENSITIVE);
Matcher mtch = ptn.matcher(line);
while (mtch.find()) {
cnt++;
}
line = in.readLine();
}
return cnt;
}
public static void main(String[] args) throws IOException {
System.out.println(lookup("WORD"));
System.out.println(lookup("word"));
System.out.println(lookup("WorD"));
System.out.println(lookup("worLd"));
}
Output:输出:
4
4
4
0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.