3

I'm trying to validate a phone number in the E.164 format using a RegEx, but it doesn't seem to be working.

Here's the RegEx:

java Pattern e164p = Pattern.compile('\+?\d{10,14}');

Error message:

Error: Compile Error: Invalid string literal '\+?\d{10,14}'. Illegal character sequence '\+' in string literal.

Any clue?

1 Answer 1

2

Double escape your backslash literals: Pattern.compile('\\+?\\d{10,14}');

(first, the double backslash sequence will be parsed on creation of the string primitive to a literal backslash, then the literal backslash escapes the + and d in your regular expression as you intend)

2
  • 1
    Thanks, working. I guess I'm too much used to PHP and JavaScript, where we normally use /\+?\d{10,14}/.
    – jpmonette
    Mar 18, 2013 at 17:47
  • 1
    Regular expressions rock as first class citizens in JS :-( I miss them too! Mar 18, 2013 at 17:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.