Stack Exchange
Regex: Unclosed character class near index
I am not very familiar with Regex and I have some issue with a regex string in Java. 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):
(?=^[\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

I think problem is in @((\[(2([0-4]\d|5[0-5])|1?\d{1,2})

Post Java code please.

What code? I just added this (?=^[\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,}))$ in http://www.regexplanet.com/advanced/java/index.html and I get this error.

It'd help if you explain what you want the regex to achieve. As it currently is, there are several errors in the pattern itself.

This pattern is used to validate an email address. We received this pattern from the web team. We have to copy this in our Android project. All we had to do was to copy and paste the received pattern in a String constant in our Android project. And I used this site http://www.regexplanet.com/advanced/java/index.html to check if the pattern is ok. It seems it's not ok for Java and I can't figure why.

http://davidcel.is/posts/stop-validating-email-addresses-with-regex/

And tell your web team to stop checking Java regex on a PCRE tester. Java has nested character class, while PCRE does not, so both [] has to be escaped inside character class.

The [ inside a character class must also be escaped.

There are multiple shortcomings in the pattern, but the severest issue is that in Java regex, you need to escape [ inside a character class.
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"".
See IDEONE demo

Thanks a lot. You saved my day. I don't know why I got 2 down votes. I just needed help with something I am not familiar to (this is why Stackoverflow exists). Thanks again for helping me.

I think your question sounds incomplete as if you wanted to do your work by us. I see you did a good job adding the additional backslashes, testing the pattern at 2 online regex testing Web sites, but forgot to escape [ inside the character class. The problem is that this won't compile and you cannot post any attempts, thus it is a valid question for me, thus, I answered.

I see now. I will try to be more specific next time. Thanks again!
