简体   繁体   English

为什么string.matches [“ +-* /”]将报告模式异常?

[英]why string.matches[“+-*/”] will report the pattern exception?

I have this code: 我有以下代码:

public static void main(String[] args) {
       String et1 = "test";
       String et2 = "test";
       et1.matches("[-+*/]"); //works fine
       et2.matches("[+-*/]"); //java.util.regex.PatternSyntaxException, why?
}

Because '-' is escape character? 因为“-”是转义字符? But why it will works fine, if '-' switchs with '+' ? 但是,如果'-'切换为'+',为什么它会正常工作呢?

it is because - is used to define a range of characters in a character class. 这是因为-用于定义字符类中的字符范围。 Since + is after * in the ascii table, the range has no sense, and you obtain an error. 由于+在ascii表中的*之后,因此该范围没有意义,并且会出现错误。

To have a literal - in the middle of a character class, you must escape it. 要具有文字-在字符类的中间,必须对其进行转义。 There is no problem if the - is at the begining or at the end of the class because it's unambigous. -是在课程的开始还是结束时没有问题,因为它是明确的。

An other situation where you don't need to escape the - is when you have a character class shortcut before, example: 不需要转义-的另一种情况是,当您之前有字符类快捷方式时,例如:

[\\d-abc]  

(other regex engines like pcre allows the same when the character class shortcut is placed after [abc-\\d] , but Java doesn't seem to allow this.) (当字符类快捷方式放在[abc-\\d]之后时,其他正则表达式引擎(例如pcre)也允许这样做,但是Java似乎不允许这样做。)

- inside a character class (the [xxx] ) is used to define a range, for example: [az] for all lower case characters. -在字符类( [xxx] )用于定义范围,例如: [az]用于所有小写字符。 If you want to actually mean "dash", it has to be in first or last position. 如果要表示“破折号”,则必须位于第一个或最后一个位置。 I generally place it first to avoid any confusions. 我通常将其放在第一位以避免任何混乱。

Alternatively you can escape it: [+\\\\-*/] . 或者,您可以对其进行转义: [+\\\\-*/]

Just FYI, the Java regular expression meta characters are defined here : 仅供参考, 此处定义了Java正则表达式元字符:

The metacharacters supported by this API are: <([{\^-=$!|]})?*+.>

As a general rule, to save myself from regexp debugging headaches, if I want to use any of these characters as a literal then I precede them with a \\ (Or \\\\ inside of a Java String expression). 作为一般规则,为了使自己免于正则表达式调试的麻烦,如果我想将这些字符中的任何一个用作文字,则可以在它们前面加上\\(或Java String表达式内的\\\\)。

Either: 要么:

et2.matches("[\\+\\-\\*/]");

Or: 要么:

et2.matches("[\\-\\+\\*/]");

Will work regardless of order. 不管顺序如何都可以工作。

我认为您应该使用:[\\-\\ + \\ * /]因为:'-'定义范围,例如:[ad]的意思是:a,b,c,d

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

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