简体   繁体   English

正则表达式:索引附近的未封闭字符类

[英]Regex: Unclosed character class near index

I am not very familiar with Regex and I have some issue with a regex string in Java. 我对Regex不太熟悉,而Java中的regex字符串也有些问题。 I get the following error: 我收到以下错误:

Unclosed character class near index 198

"(?=^[\\x00-\\x7F]+$)^(([^<>()|[\\]\\\\.,;:\\s@\\\"\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\\"\"]+)*)|(\\\"\".+\\\"\"))@((\\[(2([0-4]\\d|5[0-5])|1?\\d{1,2})(\\.(2([0-4]\\d|5[0-5])|1?\\d{1,2})){3} \\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"

Could anyone enlighten me on this issue? 有人能启发我这个问题吗?

Original received pattern from web team is this (and it's a valid regex if we check it here ): 从Web团队收到的原始模式是这样的(如果我们在此处检查,它是有效的正则表达式):

(?=^[\x00-\x7F]+$)^(([^<>()|[\]\\.,;:\s@\""]+(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$

Thanks 谢谢

There are multiple shortcomings in the pattern, but the severest issue is that in Java regex, you need to escape [ inside a character class. 模式中有多个缺点,但是最严重的问题是,在Java正则表达式中,您需要在字符类中转义[

You also can use the anchored look-ahead after ^ (no need to use two ^ in the pattern then). 您还可以在^之后使用锚定的^ (然后无需在模式中使用两个^ )。

Also, no need to use [\\""] , as ["] will already match a single " . 另外,无需使用[\\""] ,因为["]已经与单个"匹配。

String rx = "^(?=[\\x00-\\x7F]+$)(([^<>()|\\[\\]\\\\.,;:\\s@\"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@\"]+)*)|(\\\"\".+\\\"\"))@((\\[(2([0-4]\\d|5[0-5])|1?\\d{1,2})(\\.(2([0-4]\\d|5[0-5])|1?\\d{1,2})){3} \\])|(([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}))$";

Please also check \\\\\\"\\".+\\\\\\"\\" , not sure you need to match ""something here"" . 请同时检查\\\\\\"\\".+\\\\\\"\\" ,不确定是否需要匹配""something here""

See IDEONE demo IDEONE演示

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

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