简体   繁体   English

Pattern.CASE_INSENSITIVE

[英]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.

相关问题 带有Pattern.CASE_INSENSITIVE的Java RegEx性能 - Java RegEx Performance with Pattern.CASE_INSENSITIVE Java正则表达式(?i)与Pattern.CASE_INSENSITIVE - Java regex (?i) vs Pattern.CASE_INSENSITIVE 如何修复 Pattern.compile(regex, Pattern.CASE_INSENSITIVE) 上的声纳关键问题; - How to fix the Sonar critical issue on Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 不区分大小写的RegEx模式 - Case insensitive RegEx pattern GSON - 反序列化不区分大小写的字段名称(没有明确的大小写模式) - GSON - Deserialize case insensitive field name (no definite case pattern) 不区分大小写的POSIX正则表达式在Java Pattern&Matcher中不区分大小写 - Case-insensitive POSIX regex is not case-insensitive in Java Pattern & Matcher Spring Data JPA-不区分大小写的查询,带有模式匹配 - Spring Data JPA - case insensitive query w/ pattern matching 如何执行不区分大小写的模式搜索和保留大小写的替换? - How can I perform case-insensitive pattern search and case-preserving replacement? 如何为java.sql.DatabaseMetaData#getTables创建不区分大小写的模式? - How can I make a case-insensitive pattern for java.sql.DatabaseMetaData#getTables? Mapstruct 不区分大小写的映射 - Mapstruct case insensitive mapping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM