简体   繁体   English

线程“main”中的异常 java.util.regex.PatternSyntaxException:索引 9 附近的非法字符范围

[英]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]+))?)?

Your logic is unnecessarily complex and error-prone.您的逻辑不必要地复杂且容易出错。

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

What went wrong with your code?你的代码出了什么问题?

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:除此之外,下面给出了一些您应该学习/解决的更多事情:

  1. You can simplify the initialization like:您可以简化初始化,如:
String factor = "[A-Za-z]+|[0-9]+";
String mulOp = "\\*|\\/";  // '*'' or '/'
String addOp = "\\+|\\-";  // '+' or '-'
String compOp = "\\<|\\=";  // '<' or '='
  1. Change aZ to az .aZ更改为az

暂无
暂无

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

相关问题 线程“主”java.util.regex.PatternSyntaxException 中的异常:索引 2 附近的转义序列非法/不受支持 - Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 2 java.util.regex.PatternSyntaxException:索引附近非法重复 - java.util.regex.PatternSyntaxException: Illegal repetition near index java.util.regex.PatternSyntaxException:索引 12 附近的非法重复 - java.util.regex.PatternSyntaxException: Illegal repetition near index 12 java.util.regex.PatternSyntaxException:索引0附近的未闭合字符类 - java.util.regex.PatternSyntaxException: Unclosed character class near index 0 java.util.regex.PatternSyntaxException:在索引 0 + 附近悬空元字符 &#39;+&#39; - java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 + java.util.regex.PatternSyntaxException:索引附近的未闭合字符类 - java.util.regex.PatternSyntaxException: Unclosed character class near index 线程“主”中的异常java.util.regex.PatternSyntaxException: - Exception in thread “main” java.util.regex.PatternSyntaxException: java.util.regex.PatternSyntaxException:使用ant在索引0附近非法重复 - java.util.regex.PatternSyntaxException: Illegal repetition near index 0 using ant java.util.regex.PatternSyntaxException:索引12附近的未封闭字符类\\\\ b]([^。(| [] +) - java.util.regex.PatternSyntaxException: Unclosed character class near index 12 \\b]([^.(|[]+) java.util.regex.PatternSyntaxException:索引58附近的未封闭字符类错误 - java.util.regex.PatternSyntaxException: Unclosed character class near index 58 error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM