java 未知字符的正则表达式匹配器异常

java regex matcher exception on unknown character

所以我有一个字符串,我想将其拆分为不同类型的标记,作为更大的解析器的一部分。

String input = "45 + 31.05 * 110 @ 54";

我使用 javas 正则表达式库 Pattern 和 Matcher 来解释我的正则表达式并找到匹配项。

String floatRegex = "[0-9]+(\.([0-9])+)?";
String additionRegex = "[+]";
String multiplicationRegex = "[*]";
String integerRegex = "[0-9]+"

我所有的正则表达式都合并到一个主正则表达式中,在不同的正则表达式之间使用管道符号。

String masterOfRegexes = "[0-9]+(\.([0-9])+)?|[+]|[*]|[0-9]+"

我将此模式发送到 Pattern.compile() 并获取匹配器。当我从左到右 运行 matcher.find() 时,我希望得到这个结构,直到应该抛出 InvalidInputException 的“@”符号点。

[
  ["Integer": "45"],
  ["addition": "+"],
  ["Float": "31.05"],
  ["multiplication": "*"],
  ["Integer": "110"]
  Exception should be thrown...
]

问题在于 matcher.find() 完全跳过“@”符号,而是找到“@”之后的下一个整数的匹配项,即“54”。

为什么它会跳过“@”符号,我该如何做到这一点,以便在我的模式中无法识别的字符上抛出异常?

正则表达式匹配或不匹配。在您的示例数据中,它不会跳过 @,只是不匹配。

您可以做的是识别单个捕获组中的有效匹配项,并在遍历匹配项时检查组 1 是否不为空。

如果不是,则该模式具有有效的第 1 组匹配项,否则您可以抛出异常。

看到一个regex demo and a Java demo

String regex = "([0-9]+(?:\.[0-9]+)?|[+]|[*]|[0-9]+)|\S+";
String string = "45 + 31.05 * 110 @ 54";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    if (matcher.group(1) == null) {
        // your Exception here
        // throw new Exception("No match!");
        System.out.println(matcher.group() + " -> no match");
    } else {
        System.out.println(matcher.group(1) + " -> match");
    }
}

输出

45 -> match
+ -> match
31.05 -> match
* -> match
110 -> match
@ -> no match
54 -> match

Matcher知道:

  • matches:匹配全部,整个输入
  • 找到: 某处输入
  • lookingAt: 从开始,但不一定到结束

您对 find 的使用跳过了“@”。 使用罕见的 lookingAt,或检查查找 start/end 个位置。