using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"I am (?:\d+) years old";
string input = @"ex1: I am * years old
valid matches:
- ""I am 24 years old""
invalid match:
- ""I am years old""
ex2: what color is [my|your|his|her|its|our|their] (red|blue|green|yellow) *
valid matches:
- ""what color is his dog""
- ""what color is my some random text till the end of string""
ex3: [*] told me to say *
valid matches:
- ""Bob and Alice told me to say hallelujah""
- ""told me to say by nobody""";
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