Sachin Sachin - 6 months ago 11
Java Question

RegEx to validate contact number

I want to validate contact number for swedish region,
I need regx which will fulfill following conditions :


  1. Apply a filter that only allows numbers (+46xxx, 0046xxx, 0xxx).

  2. No international numbers allowed (00[other than 46], +[other than 46])

  3. Also block premium numbers (09xxx, +469xxx, 00469xxx)



I tried to generate it but it couldn't satisfy the requirement

^[+46|0046|0][0-8]{1}[0-9]{7,19}$

Answer

Replace [+46|0046|0] with (?:\+46|0046|0(?!0)) in your regex

^(?:\+46|0046|0(?!0))[0-8][0-9]{7,19}$

Regex explanation here.

Regular expression visualization


Refer : What is the difference between square brackets and parentheses in a regex?

Comments