using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(?=\d+[a-z]+(?:-\d+[a-z]+)*$)(?>(?<gal>\d+)gal-?)?(?>(?<qt>\d)+qt-?)?(?>(?<pt>\d+)pt-?)?(?>(?<cup>\d+)cup-?)?(?>(?<oz>\d+)oz-?)?$";
string input = @"1gal
1qt-2cup
1gal-6oz
1gal
6oz
1gal-1qt-2pt-2cup-5oz
1gal-1qt-2oz
1gal-1qt-2pt-2cup-5o
1gal-1qt-2oz-2cup
1gal-1qt-2oz-2cup
-1gal-1qt-2oz
1gal-1qt-2oz-
sfgsdfgdsfaf
1gal-3qt-5a
1qt2cup
1gal2qt-3pt
";
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