我有一個場景,我必須一次驗證多個電話號碼。例如,我會在網格中輸入這樣的電話號碼。如何使用正則表達式驗證多個電話號碼
+46703897733; +46733457773; +46703443832; +42708513544; +91703213815; + 919054400407。
任何人都可以幫我嗎?
在此先感謝。
我有一個場景,我必須一次驗證多個電話號碼。例如,我會在網格中輸入這樣的電話號碼。如何使用正則表達式驗證多個電話號碼
+46703897733; +46733457773; +46703443832; +42708513544; +91703213815; + 919054400407。
任何人都可以幫我嗎?
在此先感謝。
使用+46 ...號碼。 其他號碼是simlar
string regexPattern = @"^+46[0-9]{9}$";
Regex r = new Regex(regexPattern);
foreach(string s in numbers)
{
if (r.Match(s).Success)
{
Console.WriteLine("Match");
}
}
謝謝,因爲我的情況工作正常 – user1845163
這是非常硬編碼,你如何匹配這個表達式所有的數字提供的OP?對於每個不同的代碼(+42,+91等),你需要寫一個用這種方法分開正則表達式。 – RahulD
這個例子的代碼。 對於所有情況,您可以使用此代碼@「^ + [1-9] [0-9] {10} $」 –
迭代,並通過一個類似驗證它們一個:
Regex rgx = new Regex(yourPattern, RegexOptions.IgnoreCase);
foreach(string num in numbers)
{
if(rgx.Matches(num))
//do something you need
}
您還可以在表格添加的RegularExpressionValidator您的電話號碼列單元格,並通過它你的格局。然後點擊按鈕或任何導致驗證的事件都會爲您執行。
如果+是你的號碼必須比做這在C#
string[] numbers = new string[] { "+46703897733","+46733457773","46733457773"};
string regexPattern = @"^\+(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$";
Regex r = new Regex(regexPattern);
foreach(string s in numbers)
{
if (r.Match(s).Success)
{
//"+46703897733","+46733457773" are valid in this case
Console.WriteLine("Match");
}
}
如果+是不是強制性的,你可以做下面的代碼此
string regexPattern = @"^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$";
// all the numbers in the sample above will be considered as valid.
它不適合我。 – user1845163
我已驗證。你期望的輸入和輸出是什麼? – Ehsan
你的正則表達式模式應該是,然後:
[+][1-9][0-9]*
,這是你需要的;如果你想限制它,如:+911234567890,那麼你的實際應該是:
[+][1-9][0-9]{11,11}
Javascript or C#?做好決定。 –
http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – Rakesh
任何特定的模式,你想匹配? – Ehsan