帶有可選國家/地區代碼的手機號碼的正則表達式

[英]Regex for mobile number with optional country code


actually i am using it at internal application which generate a xml, whose code is written in java. But here at application if ^(249)?1\d{8}$ then it forward to next operation, if number not above then give user wrong input i.e. I am not directly interact to java codeI want a regex for mobile number validation. The regex pattern should be such that it must accept 249 country code(only once) followed by '1' (only once) followed by 8 digit. Country code should be optional.If country code doesn't exist, it should accept only 9 digit number which started with 1.

實際上我在內部應用程序中使用它生成一個xml,其代碼是用java編寫的。但是在應用程序中如果^(249)?1 \ d {8} $然后它轉發到下一個操作,如果數字不在上面然后給用戶錯誤輸入,即我不直接與java代碼交互我想要一個正則表達式用於移動號碼驗證。正則表達式模式應該是這樣的,它必須接受249國家代碼(僅一次),然后是'1'(僅一次),接着是8位數。國家/地區代碼應該是可選的。如果國家/地區代碼不存在,則只接受以1開頭的9位數字。

Also Regex which negate above condition, means any mobile number which not start with 249 and having length 12, if it of length 9 and not start with 1. The regex should accept numbers like

否定上述條件的正則表達式是指任何不以249開頭且長度為12的移動號碼,如果長度為9且不以1開頭。正則表達式應接受類似的數字

249189372837 start with 249 followed by 1 and must of 12 digit length
249123476358 
183526352  if start with 1 and must of 9 digit length (249 optional) 

Should not accept

不應該接受

2431983645238   country code not 249
24924356383799  more then 12 digit including country code
249212236472 not 1 after 249
1232345678   more length then 9
12345323     less length then 9
456278399    if 249 is not there then mut start with 1 with length of 9

So I need regex for accepting above condition and one regex which accept any number other then above accepting condition.

所以我需要正則表達式來接受上述條件和一個正則表達式,它接受除了接受條件以外的任何數字。

I tried this ^(249)([1]{1})([0-9]{8})$ when start with 249 then (1) then 8 digits and ^(1)([0-9]{8})$ when number start with 1 followed by 8 digit, but I require one regex for both condition.

我嘗試了這個^(249)([1] {1})([0-9] {8})$以249開始然后(1)然后是8位和^(1)([0-9] {8 })$當數字以1開頭后跟8位數時,但我需要一個正則表達式用於這兩個條件。

actually i am using it at internal application which generate a xml, whose code is written in java. But here at application if ^(249)?1\d{8}$ (answered by @rock321987) then it forward to next operation, if number not above regex then give user wrong input i.e. I am not directly interact to java code

實際上我在內部應用程序中使用它生成一個xml,其代碼是用java編寫的。但是在應用程序如果^(249)?1 \ d {8} $(由@ rock321987回答)然后它轉發到下一個操作,如果數字不在正則表達式之上然后給用戶錯誤輸入,即我不直接與java代碼交互

Lots of thanks in advance for any help and suggestion.

非常感謝任何幫助和建議。

1 个解决方案

#1


2  

This regex will work

這個正則表達式將起作用

^(249)?1\d{8}$

Regex Demo

For second part

第二部分

(?=^1)(^\d{1,8}$|^\d{10,}$)|(?=^2491)(^\d{1,11}$|^\d{13,}$)
(?!^(2491|1))^(\d{9}|\d{12})$

Java Code

Scanner x = new Scanner(System.in);
String pattern = "^(249)?1\\d{8}$";
Pattern r = Pattern.compile(pattern);
while (true) {
    String line = x.nextLine();
    Matcher m = r.matcher(line);
    if (m.find()) {
         System.out.println("Found -> " + m.group());
    } else {
         System.out.println("Not Found");
    }
}

Ideone Demo


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/04/20/72f122b203031d5353fa5a5c6c45667e.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com