using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?<!\d)(\d{5}(?:[-\s]\d{4})?)\b";
string input = @"""Mr. Bean bought 2 tickets 2-613-213-4567"",
""43 Butter Rd, Brossard QC K0A 3P0 – 613 213 4567"",
""Rat Race, XX, 12345"",
""Ignore phone numbers(613)2134567"",
""Grab zips with dashes 12345-6789 or no space before12345-6789"",
""Grab zips with spaces 12345 6789 or no space before12345 6789"",
""I like 1234567 dogs""";
foreach (Match m in Regex.Matches(input, pattern))
{
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