using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?x)(
# phone number
(\+1)?
(\s)?
(\(\d\d\d\))? #area code optional
(\s|-) # first separator
\d\d\d # first 3 digits
- # Separator
\d\d\d\d # last 4 digits
)";
string input = @"+1 (786) 665-5397
+1 (786) 773-7145
+1 (786) 804-8869
+1 (786) 806-5097
+1 (786) 856-7950
+1 (786) 862-2875
+1 (786) 915-7830
+1 (786) 991-4304
+1 (857) 334-1162
+1 (862) 944-0090
+1 (863) 307-5291
+1 (914) 826-4343
+1 (918) 992-1382
+1 (954) 226-7037";
RegexOptions options = RegexOptions.Multiline;
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