using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(\+?\d[\h(-]{0,3})? # Remove country code, ""+"", "" "", and ""(""
(\d{3}) # Area code
[\h)-.]{0,3} # Remove ""-"", "" "", ""."", and "")""
(\d{3}) # Three digits
[\h-.]{0,3} # Remove ""-"", "" "", and "".""
(\d{4}) # Four digits";
string substitution = @"$2.$3.$4";
string input = @"+1 123 111 1111
+1-111-111-1234
+1 (111) 111-1111
1 111 111 1111
1-111-111-1111
1-(111) 111-4567
1111111111
111.111.8765
111-345-1111";
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
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