【问题标题】:How can I print in which column number and line number where pattern has been not matched如何打印模式不匹配的列号和行号
【发布时间】:2013-04-04 10:06:48
【问题描述】:

如何打印正则表达式模式不匹配的列号和行号。

我当前的代码:

 reader = new BufferedReader(new FileReader(credentialPath));

 Pattern pattern = Pattern
                .compile(ApplicationLiterals.CREDENTIALS_URL_REG_EX);
 String line;
 while ((line = reader.readLine()) != null) {
      Matcher matcher = pattern.matcher(line.trim());
      if (matcher.find()) {
            // System.out.println(matcher.group());
            // System.out.println("**" + matcher.start());
            // System.out.println("***" + matcher.end());
            result = true;
            count1++;
       } else {
            // count1++;
            result = false;
            // System.out.println(matcher.group());
            // System.out.println(matcher.start());
            System.out.println("****problem at line number" + count1);
            break;
       }
  }

【问题讨论】:

  • 那么您在执行此操作时遇到了什么问题?
  • 我无法打印内容不匹配的确切位置。
  • 它抛出了什么错误?如果没有任何错误,那么错误的输出是什么?
  • 当我尝试打印 start end 或 group 方法时抛出非法状态异常

标签: java regex expression


【解决方案1】:

您获得IllegalStateException 的原因是matcher.group()matcher.start()

很明显,如果控件转到else 块,则意味着linePattern 不匹配。当找不到匹配项时,如何尝试打印匹配项的startgroup

异常堆栈跟踪会清楚地说明:- 找不到匹配项

如果你看到docs:-

抛出: IllegalStateException - 如果尚未尝试匹配,或者之前的匹配操作失败

在你的情况下,由于匹配失败,它会抛出IllegalStateException

正如您已经完成的那样,保留 matcher.group()matcher.start() 的注释,取消注释 count1++ 并打印 count1。它会给你行号。

更新:-

将此作为您的 else 块。

else {
    count1++;
    result = false;
    System.out.println("**This line doesn't match the pattern*** "+line);
    System.out.println("****problem at line number" + count1);
    break;
}

【讨论】:

  • 谢谢兄弟,但我知道我只是想知道如果有任何模式不匹配,我只想在这个特定的地方向用户展示你的模式不匹配。我们可以这样做吗?
  • 您可以打印 line 本身来说明该行与模式不匹配。您无法在整行中确定模式不匹配的特定列。
【解决方案2】:

如果你想显示不匹配的模式,那么你可以做两件事。

1.创建正则表达式的相反模式,并在 else 块中匹配它并显示确切的单词。例如,如果你有一个像[aeiou]* 这样的正则表达式,那么相反的就是[^aeiou]*

2. 将matcher.start() and matcher.end() 与相同的变量保持一致,并在 else 块中使用这些变量来查找行中出现不匹配的其余部分。假设如果你结束 20 并且在循环的下一次迭代中它来到 else 块,这意味着 20 之后有不匹配的,所以在 20 之后显示行的内容。

编辑:

从流动代码中获得帮助

public static void main(String[] args) {
    String source = "Java is best \n Test Java is good \n  Java Hello";
    Pattern pattern = Pattern.compile("Java");
    Matcher matcher = null;
    Scanner scanner = new Scanner(source);
    String line = null;
    int end = 0;
    int lineNumber = 0;
    while (scanner.hasNextLine()) {
        line = scanner.nextLine();
        matcher = pattern.matcher(line);
        ++lineNumber;
        while (matcher.find()) {
            System.out.println(matcher.group());
            end = matcher.end();
        }
        if (end < line.length() - 1) {
            System.out.println("NOt matched Line :" + lineNumber + " Words:-"
                    + line.substring(end));
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2019-04-30
    相关资源
    最近更新 更多