3

I have the below RegEx to match email address

[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}

AM trying to escape the characters as below but am still getting java compilation error. Could someone please help me to escape characters in the above RegEx?

String pattern = "\\[-0-9a-zA-Z\\.\\+_\\]\\+@\\[-0-9a-zA-Z\\.\\+_\\]+\\\.\\[a-zA-Z\\]\\{2,4\\}";

1 Answer 1

6

The escaped regex is:

"[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\\.[a-zA-Z]{2,4}"
Sign up to request clarification or add additional context in comments.

1 Comment

... the point being that characters like . and + don't need to be escaped at all inside square bracket character classes as they are treated as literals by default. The only thing that needs to be escaped from Java is the backslash before the literal dot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.