"^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4})|((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{5})|((\+44\s?\d{5}|\(?0\d{5}\)?)\s?\d{5})|((\+44\s?\d{5}|\(?0\d{5}\)?)\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$";
I use the above pattern to validate UK telephone number. It’s working in JavaScript (client side). but its not working in Java code (server side).
The error message says invalid escape sequence.
You need to replace all your
\with\\Having said that, it seems like your regex is a bit complicated for a phone-number?
I see that the country-code for GB (+44) is included quite a few times, so I guess you could be able to say that only once:
And the rest of the regex where the elipsis are.
Another idea to make this clearer is to break this regex into several ones for each case like:
In my opinion this code would be much simpler to maintain.