Programming

유효한 국제 전화 번호와 일치하는 정규 표현식은 무엇입니까?

procodes 2020. 8. 27. 22:28
반응형

유효한 국제 전화 번호와 일치하는 정규 표현식은 무엇입니까?


전화를 걸기 전에 전화 번호가 유효한지 확인해야합니다. 전화는 전 세계 어디로 든 갈 수 있습니다.

유효한 국제 전화 번호와 일치하는 정규 표현식은 무엇입니까?


\+(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{1,14}$

일반 국제 전화 번호를 일치시키기위한 올바른 형식입니다. 미국 유선 중심의 국제 액세스 코드 011을 표준 국제 액세스 코드 식별자 '+'로 대체하여 필수로 지정했습니다. 또한 국가 번호의 최소값을 한 자리 이상으로 변경했습니다.

이 형식의 번호를 휴대폰 주소록에 입력하면 여행지에 관계없이 주소록에있는 모든 번호로 성공적으로 전화를 걸 수 있습니다. 유선 전화의 경우 플러스를 전화를 거는 국가의 국제 액세스 코드로 바꿉니다.

이것은 국가 번호 계획 규칙을 고려하지 않습니다. 특히 국가 번호 계획이 허용하지 않는 위치에서 0과 1을 허용하고 일부 국가 (예 : 미국)의 국가 번호 계획보다 긴 번호 길이를 허용합니다.


모든 국가 코드는 ITU에서 정의합니다. 다음 정규식은 ITU-T E.164 및 ITU Operational Bulletin No. 930 – 15.IV.2009의 부록을 기반으로 합니다. 여기에는 향후 사용을 위해 예약 된 모든 현재 국가 코드와 코드가 포함되어 있습니다. 조금 짧아 질 수 있지만 각 코드를 독립적으로 포함하기로 결정했습니다.

미국에서 거는 전화 용입니다. 다른 국가의 경우 국제 액세스 코드 (정규식 시작 부분의 011)를 해당 국가의 전화 걸기 계획에 적합한 것으로 바꾸십시오 .

또한 ITU E.164는 전체 국제 전화 번호의 최대 길이를 15 자리로 정의합니다. 즉, 3 자리 국가 코드를 사용하면 최대 12 자리가 추가되고 1 자리 국가 코드에는 최대 14 자리가 추가 될 수 있습니다. 따라서

[0-9]{0,14}$

정규식의 끝입니다.

가장 중요한 것은이 정규식이 번호가 유효 함을 의미하지 않는다는 것입니다. 각 국가는 자체 내부 번호 지정 계획을 정의합니다. 이것은 국가 코드가 유효한지 확인하기위한 것입니다.

262 | 261 | 260 | 259 | 258 | 257 | 256 | 255 | 254 | 253 | 252 | 251 | 250 | 249 | 248 | 247 | 246 | 245 | 244 | 243 | 242 | 241 | 240 | 239 | 238 | 237 | 236 | 235 | 234 | 233 | 232 | 231 | 230 | 229 | 228 | 227 | 226 | 225 | 224 | 223 | 222 | 221 | 220 | 219 | 218 | 217 | 216 | 215 | 214 | 213 | 212 | 211 | 210 | 98 | 95 | 94 | 93 | 92 | 91 | 90 | 86 | 84 | 82 | 81 | 66 | 65 | 64 | 63 | 62 | 61 | 60 | 58 | 57 | 56 | 55 | 54 | 53 | 52 | 51 | 49 | 48 | 47 | 46 | 45 | 44 | 43 | 41 | 40 | 39 | 36 | 34 | 33 | 32 | 31 | 30 | 27 | 20 | 7 | 1) [0-9] {0, 14} $


이것은 추가 최적화입니다.

\+(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)
\W*\d\W*\d\W*\d\W*\d\W*\d\W*\d\W*\d\W*\d\W*(\d{1,2})$

(i) 유효한 국제 접두어
(ii) 뒤에 9 자리 또는 10 자리 숫자, 구분 기호의 유형 또는 배치 (마지막 두 자리 제외)를 허용합니다.

일치합니다 :
+ 1-234-567-8901
+ 61-234-567-89-01
+ 46-234 5678901
+1 (234) 56
89901 +1 (234) 56-89901
+46.234.567.8901
+1 / 234 / 567 / 8901


Google의 libphonenumber 라이브러리를 사용할 수 있습니다 .

PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
String decodedNumber = null;
PhoneNumber number;
    try {
        number = phoneNumberUtil.parse(encodedHeader, null);
        decodedNumber = phoneNumberUtil.format(number, PhoneNumberFormat.E164);
    } catch (NumberParseException e) {
        e.printStackTrace();
    }

나는 이것을 사용한다 :

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

장점 : + 또는 011 시작을 인식하고 필요한만큼 길게 만들며 많은 확장 규칙을 처리합니다. (#, x, ext, extension)


이것은 국제 전화 번호에 적용됩니다.

씨#:

@"^((\+\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}$/

다음은 정규식의 "최적화 된"버전입니다.

^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.


For iOS SWIFT I found this helpful,

let phoneRegEx = "^((\\+)|(00)|(\\*)|())[0-9]{3,14}((\\#)|())$"

Modified @Eric's regular expression - added a list of all country codes (got them from xxxdepy @ Github. I hope you will find it helpful:

/(\+|00)(297|93|244|1264|358|355|376|971|54|374|1684|1268|61|43|994|257|32|229|226|880|359|973|1242|387|590|375|501|1441|591|55|1246|673|975|267|236|1|61|41|56|86|225|237|243|242|682|57|269|238|506|53|5999|61|1345|357|420|49|253|1767|45|1809|1829|1849|213|593|20|291|212|34|372|251|358|679|500|33|298|691|241|44|995|44|233|350|224|590|220|245|240|30|1473|299|502|594|1671|592|852|504|385|509|36|62|44|91|246|353|98|964|354|972|39|1876|44|962|81|76|77|254|996|855|686|1869|82|383|965|856|961|231|218|1758|423|94|266|370|352|371|853|590|212|377|373|261|960|52|692|389|223|356|95|382|976|1670|258|222|1664|596|230|265|60|262|264|687|227|672|234|505|683|31|47|977|674|64|968|92|507|64|51|63|680|675|48|1787|1939|850|351|595|970|689|974|262|40|7|250|966|249|221|65|500|4779|677|232|503|378|252|508|381|211|239|597|421|386|46|268|1721|248|963|1649|235|228|66|992|690|993|670|676|1868|216|90|688|886|255|256|380|598|1|998|3906698|379|1784|58|1284|1340|84|678|681|685|967|27|260|263)(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{4,20}$/

There's obviously a multitude of ways to do this, as evidenced by all of the different answers given thus far, but I'll throw my $0.02 worth in here and provide the regex below, which is a bit more terse than nearly all of the above, but more thorough than most as well. It also has the nice side-effect of leaving the country code in $1 and the local number in $2.

^\+(?=\d{5,15}$)(1|2[078]|3[0-469]|4[013-9]|5[1-8]|6[0-6]|7|8[1-469]|9[0-58]|[2-9]..)(\d+)$


A simple version for european numbers, that matches numbers like 0034617393211 but also long ones as 004401484172842.

^0{2}[0-9]{11,}

Hope it helps :·)


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();
}

Here is a regex for the following most common phone number scenarios. Although this is tailored from a US perspective for area codes it works for international scenarios.

  1. The actual number should be 10 digits only.
  2. For US numbers area code may be surrounded with parentheses ().
  3. The country code can be 1 to 3 digits long. Optionally may be preceded by a + sign.
  4. There may be dashes, spaces, dots or no spaces between country code, area code and the rest of the number.
  5. A valid phone number cannot be all zeros.

    ^(?!\b(0)\1+\b)(\+?\d{1,3}[. -]?)?\(?\d{3}\)?([. -]?)\d{3}\3\d{4}$
    

Explanation:

    ^ - start of expression  
    (?!\b(0)\1+\b) - (?!)Negative Look ahead. \b - word boundary around a '0' character. \1 backtrack to previous capturing group (zero). Basically don't match all zeros.  
    (\+?\d{1,3}[. -]?)? - '\+?' plus sign before country code is optional.\d{1,3} - country code can be 1 to 3 digits long. '[. -]?' - spaces,dots and dashes are optional. The last question mark is to make country code optional.  
    \(?\d{3}\)? - '\)?' is to make parentheses optional. \d{3} - match 3 digit area code.  
    ([. -]?) - optional space, dash or dot
    $ - end of expression

More examples and explanation - https://regex101.com/r/hTH8Ct/2/


Try the following API for phone number validation. Also this will return the Country, Area and Provider

demo https://libphonenumber.appspot.com/

git https://github.com/googlei18n/libphonenumber/releases/tag/v8.9.0


The international numbering plan is based on the ITU E.164 numbering plan. I guess that's the starting point to your regular expression.

I'll update this if I get around to create a regular expression based on the ITU E.164 numbering.


This Regex Expression works for India, Canada, Europe, New Zealand, Australia, United States phone numbers, along with their country codes:

"^(\+(([0-9]){1,2})[-.])?((((([0-9]){2,3})[-.]){1,2}([0-9]{4,10}))|([0-9]{10}))$"


This works for me, without 00, 001, 0011 etc prefix though:

/^\+*(\d{3})*[0-9,\-]{8,}/

Even though this is not really using RegExp to get the job done - or maybe because of that - this looks like a nice solution to me: https://intl-tel-input.com/node_modules/intl-tel-input/examples/gen/is-valid-number.html


Try this, it works for me.

^(00|\+)[1-9]{1}([0-9][\s]*){9,16}$

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.


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

^(00|\+){1}\d{12}$

참고URL : https://stackoverflow.com/questions/2113908/what-regular-expression-will-match-valid-international-phone-numbers

반응형