Separating (+) from a Phone Number

There are lots of ways to do this using string manipulation but probably the best is using a text parser and regular expression parser especially since there are many conditions such as

  • does the + and the code have a space or are next to each other
  • is the code next to other data or is there a space
  • what are the valid codes or are you validating on a number of any length.

If we assume a properly formatted international phone number this regex will work. The first two capture groups will extract the + and code and the last capture group will extract the phone number

(\+?)(\d{1,4}?[-.\s]?\(?\d{1,3}?\)?)[-.\s]?(\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9})

More details on REGEx101.

3 Likes