[英]Exception in thread “main” java.util.regex.PatternSyntaxException: Illegal character range near index 9
I want to match alphanumeric words separated by the operators, +, -, *, /, <, > and ending with a semicolon.我想匹配由运算符、+、-、*、/、<、> 分隔并以分号结尾的字母数字单词。 There can be whitespace characters in between eg the following strings should return true
:之间可以有空格字符,例如以下字符串应返回true
:
first + second;
first - second;
first * second;
first / second;
first;
first + second < third;
third < second * first;
This is what I have tried:这是我尝试过的:
public boolean isExpr(String line) {
// factor = ([A-Za-Z]+|[0-9]+) for example: aasdaa or 23131 or xyz or 1 or a
// simple-expr = (factor {mulop factor} {addop factor {mulop factor}})
// expr = simple-expr compop simple-expr | simple-expr
String factor = new String("([A-Za-Z]+|[0-9]+)");
String mulOp = new String("(\\*|\\/)"); // '*'' or '/'
String addOp = new String("(\\+|\\-)"); // '+' or '-'
String compOp = new String("(\\<|\\="); // '<' or '='
String simpleExpr = new String("(" + factor + " (" + mulOp + " " + factor + ")? (" + addOp + " " + factor + " (" + mulOp + " " + factor + ")?)?");
String expr = new String("(" + simpleExpr + " " + compOp + " " + simpleExpr + ")|" + simpleExpr);
System.out.println(line.matches(expr));
return line.matches(expr);
}
What is wrong with that code and how can I solve it?该代码有什么问题,我该如何解决?
I got the below error on executing my code:执行我的代码时出现以下错误:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 9
((([A-Za-Z]+|[0-9]+) ((\*|\/) ([A-Za-Z]+|[0-9]+))? ((\+|\-) ([A-Za-Z]+|[0-9]+) ((\*|\/) ([A-Za-Z]+|[0-9]+))?)? (\<|\= (([A-Za-Z]+|[0-9]+) ((\*|\/) ([A-Za-eZ]+|[0-9]+))? ((\+|\-) ([A-Za-Z]+|[0-9]+) ((\*|\/) ([A-Za-Z]+|[0-9]+))?)?)|(([A-Za-Z]+|[0-9]+) ((\*|\/) ([A-Za-Z]+|[0
-9]+))? ((\+|\-) ([A-Za-Z]+|[0-9]+) ((\*|\/) ([A-Za-Z]+|[0-9]+))?)?
I suggest you, instead of using unnecessarily complex and error-prone logic, simply use the regex , [A-Za-z0-9]+(?:\\s*[\\/*+\\-<>]\\s*[A-Za-z0-9]+\\s*)*;
我建议你,不要使用不必要的复杂和容易出错的逻辑,只需使用正则表达式[A-Za-z0-9]+(?:\\s*[\\/*+\\-<>]\\s*[A-Za-z0-9]+\\s*)*;
which covers all the example strings you have posted in the question.它涵盖了您在问题中发布的所有示例字符串。
Explanation of the regex:正则表达式的解释:
[A-Za-z0-9]+
: 1+ alphabets or digits [A-Za-z0-9]+
: 1+ 字母或数字(?:
: Open non-capturing group (?:
::: 打开非捕获组
\\s*
: 0+ whitespace characters \\s*
: 0+ 个空白字符[\\/*+\\-<>]
: One of /
, *
, +
, -
, <
, >
[\\/*+\\-<>]
: /
、 *
、 +
、 -
、 <
、 >
\\s*
: 0+ whitespace characters \\s*
: 0+ 个空白字符[A-Za-z0-9]+
: 1+ alphabets or digits [A-Za-z0-9]+
: 1+ 字母或数字\\s*
: 0+ whitespace characters \\s*
: 0+ 个空白字符)
: Close non-capturing group )
: 关闭非捕获组*
: Quantifier to make the non-capturing group match 0+ times *
: 使非捕获组匹配 0+ 次的量词;
: The charcter literal, ;
: 字符字面量, ;
Demo:演示:
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
// Test
Stream.of(
"first + second;",
"first * second;",
"first - second;",
"first / second;",
"first;",
"first + second < third;",
"third < second * first;"
).forEach(s -> System.out.println(isExpr(s)));
}
public static boolean isExpr(String line) {
return line.matches("[A-Za-z0-9]+(?:\\s*[\\/*+\\-<>]\\s*[A-Za-z0-9]+\\s*)*;");
}
}
Output:输出:
true
true
true
true
true
true
true
Because of the unnecessarily complex logic that you have implemented, one or more of the parentheses in the final regex have not been closed.由于您实现了不必要的复杂逻辑,最终正则表达式中的一个或多个括号尚未关闭。 In addition to that, I can see at least one part where the parenthesis has not been closed eg除此之外,我至少可以看到括号没有被关闭的一部分,例如
String compOp = new String("(\\<|\\="); // '<' or '='
It should be它应该是
String compOp = new String("(\\<|\\=)"); // '<' or '='
//----------------------------------^
Apart from this, given below are a couple of more things that you should learn/address:除此之外,下面给出了一些您应该学习/解决的更多事情:
String factor = "[A-Za-z]+|[0-9]+";
String mulOp = "\\*|\\/"; // '*'' or '/'
String addOp = "\\+|\\-"; // '+' or '-'
String compOp = "\\<|\\="; // '<' or '='
aZ
to az
.将aZ
更改为az
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.