繁体   English   中英

Java pattern.compile中的表达式

[英]expression in java pattern.compile

我花了两次时间从Java Pattern中获取特定值。 它在第一个中工作正常,但在第二个中则无效。

我的密码

public static void main(String[] args) throws IOException {
    final String path = "E://farhad.txt";
    FileReader fr = new FileReader(path);
    BufferedReader br = new BufferedReader(fr);
    String line;
    Pattern pattern = Pattern.compile("\\bCBM_Component_", Pattern.CASE_INSENSITIVE);
    while ((line = br.readLine()) != null) {
        Matcher matcher = pattern.matcher(line);
        String[] splitArray = new String[3];
        while (matcher.find()) {
            int i = 0;
            for (String retval : line.split("\"", 3)) {
                splitArray[i] = retval;
                i++;
            }
            System.out.println("CBM name: " + splitArray[1]);
            System.out.println("CBM number: " + splitArray[2].substring(2, splitArray[2].length() - 1));

            line = br.readLine();
            pattern = Pattern.compile("\\bAccountability:\\b", Pattern.CASE_INSENSITIVE);
            matcher = pattern.matcher(line);
            while (!matcher.find()) {
                line = br.readLine();
            }
        }
    }
    fr.close();
}

}

我的文件

View of the Model "CBM_Component_Account Reconciliation" (CBP10609)
===================================================================
      CBM Component
      Accountability: Execute
      Control Element:  Accounting Entity to Accounting Entity
      Relationship

从第一图案( CBM_Component_ )它工作得很好,但在第二个( Accountability: )它没有做任何事情。 我该怎么办。 谢谢

问题归结为-为什么Pattern.compile("\\\\bAccountability:\\\\b", Pattern.CASE_INSENSITIVE)与文本不匹配: Accountability: Execute

这是因为您错误地使用了单词边界匹配器\\b (在模式末尾“:”字符之后)。 当您从图案的末端删除边界匹配器时,您将获得预期的结果。

有关单词边界匹配器如何工作的更多信息: 在Java中使用\\ b边界匹配器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM