Q:

JS regex to Java, syntax issues

I have this to big regex that I use in JavaScript to validate email address.
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

When I try to used them in Java, I get syntax errors.

Unclosed character class near index 154 /^(([^<>()[]\.,;:\s@\"]+(.[^<>()[]\.,;:\s@\"]+)*)|(\".+\"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/ ^

I'm using http://www.regexplanet.com/ to test the regex.
Is there any way to convert or escape chars or something like this? Thanks!

A:

This is the JS regex as a Java string literal:
"(([^<>()\\[\\].,;:\\s@\"]+(\\.[^<>()\\[\\].,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}))"

The anchors (^$) have been removed, since they're not needed when calling matches().
All \, except \", have been doubled.
[ inside a class ([]) needed escaping, so escape added (\\[).
. inside a class ([]) don't need escaping, so escape removed.
- inside a class ([]) don't need escaping if placed last, so moved to end.

