What regular expression will match valid internati

2019-01-01 10:09发布

I need to determine whether a phone number is valid before attempting to dial it. The phone call can go anywhere in the world.

What regular expression will match valid international phone numbers?

21条回答
梦寄多情
2楼-- · 2019-01-01 10:49

I have used this below:

^(\+|00)[0-9]{1,3}[0-9]{4,14}(?:x.+)?$

The format +CCC.NNNNNNNNNNxEEEE or 00CCC.NNNNNNNNNNxEEEE

Phone number must start with '+' or '00' for an international call. where C is the 1–3 digit country code,

N is up to 14 digits,

and E is the (optional) extension.

The leading plus sign and the dot following the country code are required. The literal “x” character is required only if an extension is provided.

查看更多
浪荡孟婆
3楼-- · 2019-01-01 10:50

This will work for international numbers;

C#:

@"^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$"

JS:

/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
查看更多
余生无你
4楼-- · 2019-01-01 10:54

I use this one:

/([0-9\s\-]{7,})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/

Advantages: recognizes + or 011 beginnings, lets it be as long as needed, and handles many extension conventions. (#,x,ext,extension)

查看更多
千与千寻千般痛.
5楼-- · 2019-01-01 10:55

Here's an "optimized" version of your regex:

^011(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{0,14}$

You can replace the \ds with [0-9] if your regex syntax doesn't support \d.

查看更多
人间绝色
6楼-- · 2019-01-01 10:55
public static boolean validateInternationalPhoneNumberFormat(String phone) {
    StringBuilder sb = new StringBuilder(200);

    // Country code
    sb.append("^(\\+{1}[\\d]{1,3})?");

    // Area code, with or without parentheses
    sb.append("([\\s])?(([\\(]{1}[\\d]{2,3}[\\)]{1}[\\s]?)|([\\d]{2,3}[\\s]?))?");

    // Phone number separator can be "-", "." or " "

    // Minimum of 5 digits (for fixed line phones in Solomon Islands)
    sb.append("\\d[\\-\\.\\s]?\\d[\\-\\.\\s]?\\d[\\-\\.\\s]?\\d[\\-\\.\\s]?\\d[\\-\\.\\s]?");

    // 4 more optional digits
    sb.append("\\d?[\\-\\.\\s]?\\d?[\\-\\.\\s]?\\d?[\\-\\.\\s]?\\d?$");

    return Pattern.compile(sb.toString()).matcher(phone).find();
}
查看更多
裙下三千臣
7楼-- · 2019-01-01 10:57

Try this, I do not know if there is any phone number longer than 12:

^(00|\+){1}\d{12}$
查看更多
登录 后发表回答