using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^ # Start of string
(?: # A container group:
(\+49|0049|\+\(49\)|\(\+49\))? [ ()\/-]* # German: country code
(?(1)|0)1(?:5[12579]|6[023489]|7[0-9]) # trunk prefix and company code
| # or
(\+43|0043|\+\(43\)|\(\+43\))? [ ()\/-]* # Austrian: country code
(?(2)|0)6(?:64|(?:50|6[0457]|7[0678]|8[0168]|9[09])) # trunk prefix and company code
)
[ ()\/-]* # zero or more spaces, parens, / and -
\d(?:[ \/-]*\d){6,7} # a digit and then six or seven occurrences of space, / or - and a digit
\s* # zero or more whites
$ # end of string";
string input = @"################################### This is allowed ##########################
#### Longes German format ### https://en.wikipedia.org/wiki/Telephone_numbers_in_Germany
017698600189
0176 98600189
0176 98 600 189
+4917698600189
+49 17698600189
0049 17698600189
004917698600189
(+49)17698600189
(+49) 17698600189
+(49)17698600189
+(49) 17698600189
+(49) 176 98600189
+(49) 176 98 600 189
+49 176 98 600 189
+49 (176) 98 600 189
#### Shortest German format ### https://en.wikipedia.org/wiki/Telephone_numbers_in_Germany
01609860018
0160 9860018
0160 98 600 18
+491609860018
+49 1609860018
0049 1609860018
00491609860018
(+49)1609860018
(+49) 1609860018
+(49)1609860018
+(49) 1609860018
+(49) 160 9860018
+(49) 160 98 600 18
+49 160 98 600 18
+49 (160) 98 600 18
#### Different Prefix https://en.wikipedia.org/wiki/Telephone_numbers_in_Germany
017698600189
01609860018
01519860018
01709860018
01719860018
01759860018
01529860018
01799860018
01789860018
01779860018
01559860018
01629860018
01729860018
01739860018
01749860018
### Austrian numbers in different possible formats: https://www.tarife.at/ratgeber/vorwahl-handynetze
06641234567
066412345678
+436641234567
+4366412345678
0664 1234567
0664 12345678
0664 123 456 7
0664 123 456 78
0664 123 4567
0664 123 45678
+43 664 1234567
+43 664 12345678
+43 6641234567
+43 66412345678
+43 664 123 4567
+43 664 123 45678
+43 664 123 456 7
+43 664 123 456 78
(+43) 664 123 456 7
(+43) 664 123 456 78
+(43) 664 123 456 7
+(43) 664 123 456 78
### Austrian numbers with same format but different prefix
06801234567
068012345678
06881234567
068812345678
06811234567
068112345678
06991234567
069912345678
06641234567
066412345678
06811234567
068112345678
06671234567
066712345678
06501234567
065012345678
06761234567
067612345678
06501234567
065012345678
06781234567
067812345678
06771234567
067712345678
06901234567
069012345678
06651234567
066512345678
06861234567
068612345678
############################################################################################
############################################################################################
################################### This is NOT valid ######################################
012345678901234
123w345345345345
0123456789101191919
### Too short
0160986001
016098600
01609860
0160986
016098
01609
0160
016
01
0
### Too short with +49 (same is true with +43)
+490160986001
+49016098600
+4901609860
+490160986
+49016098
+4901609
+490160
+49016
+4901
+490
### Too short with 0049 (same is true with 0043)
00490160986001
0049016098600
004901609860
00490160986
0049016098
004901609
00490160
0049016
004901
00490
### Too long for Germany
0176123456789
01761234567891
017612345678912
0176123456789123
01761234567891234
017612345678912345
0176123456789123456
+49176123456789
+491761234567891
+4917612345678912
+49176123456789123
+491761234567891234
+4917612345678912345
+49176123456789123456
0049176123456789
00491761234567891
004917612345678912
0049176123456789123
00491761234567891234
004917612345678912345
0049176123456789123456
### Not German and not Austrain format for mobile numbers
+12127319863
+13322014056
+12126712234
+427532697710
+417868150810
+287533002875
";
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx