0

I got this Javascript phone regex: /^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/

String "063-444-444" passes succesfully.

I tried to convert it to Java:

^([\\+][0-9]{1,3}[ \\.\\-])?([\\(]{1}[0-9]{2,6}[\\)])?(\\[0-9 \\.\\-\\]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$ 

The same String fails in Java. What's wrong?

1 Answer 1

4

You haven't escaped it correctly. You don't need to escape the brackets ([ and ]) around the character classes.

Try:

^([\\+][0-9]{1,3}[ \\.\\-])?([\\(]{1}[0-9]{2,6}[\\)])?([0-9 \\.\\-\\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I escaped [ and ] because I had a runtime exception "Unclosed character class near xxx". Anyway, this seems to work.

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.