Xanathos
Xanathos

Reputation: 598

Translate phone-number regex to java

I found this regex in a plugin called hi5validator for jQuery, and I found it pretty good, I'm already using it on JavaScript:

/^([\+][0-9]{1,3}([ \.\-])?)?([\(][0-9]{1,6}[\)])?([0-9 \.\-]{1,32})(([A-Za-z \:]{1,11})?[0-9]{1,4}?)$/

I wanted to use this regex but in Java, and I tried to do this same thing with another regex in that library, but when I used an online evaluator, the expression gave lots of trouble. Fortunately, i found another regex that helped with that.

As for this one, can someone give me the proper Java version?

Upvotes: 0

Views: 71

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

The logic of your regex is fine - you need to fix some minor details:

  • Put double quotes " instead of slashes / around your regex
  • Do not escape with back slashes parentheses ( ), dashes - in trailing positions, pluses +, colons :, and dots . inside character classes (I am not sure if it is necessary to escape these characters in Javascript either).

Here is what you should get:

"^([+][0-9]{1,3}([ .-])?)?([(][0-9]{1,6}[)])?([0-9 .-]{1,32})(([A-Za-z :]{1,11})?[0-9]{1,4}?)$"

Upvotes: 3

Related Questions