Дизайн и тестирование каждого рисунка sep artely:
Format Pattern
-------------------------------------------------
xxxxxxxxxx ^[0-9]{10}$
+xx xx xxxxxxxx ^\+[0-9]{2}\s+[0-9]{2}\s+[0-9]{8}$
xxx-xxxx-xxxx ^[0-9]{3}-[0-9]{4}-[0-9]{4}$
....
затем объединить их в окончательный один:
(pattern1)|(pattern2)|...|(patternN)
Для трех моделей выше комбинированного шаблон
(^[0-9]{10}$)|(^\+[0-9]{2}\s+[0-9]{2}[0-9]{8}$)|(^[0-9]{3}-[0-9]{4}-[0-9]{4}$)
вы можете реализовать что-то вроде этого:
//TODO: you may want to load the patterns supported from resource, file, settings etc.
private static string[] m_Patterns = new string[] {
@"^[0-9]{10}$",
@"^\+[0-9]{2}\s+[0-9]{2}[0-9]{8}$",
@"^[0-9]{3}-[0-9]{4}-[0-9]{4}$",
};
private static string MakeCombinedPattern() {
return string.Join("|", m_Patterns
.Select(item => "(" + item + ")"));
}
Тесты:
string[] tests = new string[] {
"9088534816",
"+91 33 40653155",
"033-2647-0969",
"123",
"12115351689385",
};
var result = string
.Join(Environment.NewLine, tests
.Select(test =>
$"{test,18} {(Regex.IsMatch(test, MakeCombinedPattern()) ? "yes" : "no"),3}"));
Console.Write(result);
Результат:
9088534816 yes
+91 33 40653155 yes
033-2647-0969 yes
123 no
12115351689385 no
Ваши первые попытки? –
Да, это мой \ +? \ D [\ d -] {8,12} \ d –
Дизайн и тестирование каждого шаблона * отдельно *: '^ [0-9] {10} $', '^ \ + [0-9] {2} \ s + [0-9] {2} \ s + [0-9] {8} $ 'и т. Д. Затем * объединить * их:'^([0-9] {10}) | (\ + [0-9] {2} \ s + [0-9] {2} \ s + [0-9] {8}) | ... | (...) $ ' –