diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.cs index 60790fb89acbd6..1a228d68ad51d8 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.cs +++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.cs @@ -82,6 +82,21 @@ protected Regex() /// Initializes a new instance of the class for the specified regular expression. /// /// The regular expression pattern to match. + /// + /// The parameter consists of regular expression language elements that symbolically + /// describe the string to match. For more information about regular expressions, see + /// Regular Expression Language - Quick Reference. + /// Calling the constructor is equivalent to calling the + /// constructor with a value of for the options argument. + /// A object is immutable, which means it can be used only for the match pattern you define when you create it. + /// However, it can be used any number of times without being recompiled. + /// This constructor creates a case-sensitive regular expression. For a case-insensitive match, use the + /// constructor. + /// + /// + /// The following example uses this constructor to create a regular expression that matches words beginning with the letters "a" or "t". + /// + /// /// A regular expression parsing error occurred. /// is . public Regex([StringSyntax(StringSyntaxAttribute.Regex)] string pattern) : @@ -95,6 +110,17 @@ public Regex([StringSyntax(StringSyntaxAttribute.Regex)] string pattern) : /// /// The regular expression pattern to match. /// A bitwise combination of the enumeration values that modify the regular expression. + /// + /// The parameter consists of regular expression language elements that symbolically + /// describe the string to match. For more information about regular expressions, see + /// Regular Expression Language - Quick Reference. + /// A object is immutable, which means it can be used only for the match parameters you define when you create it. + /// However, it can be used any number of times without being recompiled. + /// + /// + /// The following example uses this constructor to create a case-insensitive regular expression that matches words beginning with the letters "a" or "t". + /// + /// /// A regular expression parsing error occurred. /// is . /// @@ -115,6 +141,37 @@ public Regex([StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string /// /// A time-out interval, or to indicate that the method should not time out. /// + /// + /// The parameter consists of regular expression language elements that symbolically + /// describe the string to match. For more information about regular expressions, see + /// Regular Expression Language - Quick Reference. + /// A object is immutable, which means it can be used only for the match pattern you define when you create it. + /// However, it can be used any number of times without being recompiled. + /// The parameter specifies how long a pattern-matching method should try to find a match before it times out. + /// If no match is found in that time interval, the pattern-matching method throws a . + /// The instance pattern-matching methods that observe the interval include: + /// + /// + /// + /// + /// + /// + /// + /// + /// Setting a time-out interval can help prevent regular expressions that rely on excessive backtracking from appearing + /// to stop responding when they process input that contains near matches. For more information, see + /// Best practices for regular expressions in .NET and + /// Backtracking in regular expressions. + /// When choosing a time-out interval, consider the following factors: + /// + /// The length and complexity of the regular expression pattern. + /// The expected machine load. + /// + /// + /// + /// The following example creates a object with a very short initial time-out and retries with a larger one if a timeout occurs. + /// + /// /// A regular expression parsing error occurred. /// is . /// diff --git a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Examples.cs b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Examples.cs index 3c391a7e50196c..4e4da89e538dc6 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Examples.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Examples.cs @@ -28,4 +28,88 @@ public static void MatchZipCode() Assert.Equal("98052", match.Value); } } + + public class RegexConstructorExamples + { + [Fact] + public static void ConstructorWithPattern() + { + #region RegexCtorString + string pattern = @"\b[at]\w+\b"; + string input = "The archive was trimmed and tagged."; + MatchCollection matches = new Regex(pattern).Matches(input); + foreach (Match match in matches) + Console.WriteLine(match.Value); + + // This code prints the following output: + // + // archive + // trimmed + // and + // tagged + #endregion + + Assert.Equal(4, matches.Count); + Assert.Equal("archive", matches[0].Value); + Assert.Equal("trimmed", matches[1].Value); + Assert.Equal("and", matches[2].Value); + Assert.Equal("tagged", matches[3].Value); + } + + [Fact] + public static void ConstructorWithPatternAndOptions() + { + #region RegexCtorStringOptions + string pattern = @"\b[at]\w+\b"; + string input = "The archive was trimmed and tagged."; + MatchCollection matches = new Regex(pattern, RegexOptions.IgnoreCase).Matches(input); + foreach (Match match in matches) + Console.WriteLine(match.Value); + + // This code prints the following output: + // + // The + // archive + // trimmed + // and + // tagged + #endregion + + Assert.Equal(5, matches.Count); + Assert.Equal("The", matches[0].Value); + Assert.Equal("archive", matches[1].Value); + Assert.Equal("trimmed", matches[2].Value); + Assert.Equal("and", matches[3].Value); + Assert.Equal("tagged", matches[4].Value); + } + + [Fact] + public static void ConstructorWithPatternOptionsAndMatchTimeout() + { + #region RegexCtorStringOptionsMatchTimeout + string pattern = @"(a+)+$"; + string input = new string('a', 15) + "!"; + TimeSpan timeout = TimeSpan.FromTicks(1); + bool isMatch; + + try + { + isMatch = new Regex(pattern, RegexOptions.None, timeout).IsMatch(input); + } + catch (RegexMatchTimeoutException) + { + timeout = TimeSpan.FromSeconds(3); + isMatch = new Regex(pattern, RegexOptions.None, timeout).IsMatch(input); + } + + Console.WriteLine($"Match found: {isMatch}"); + + // This code prints the following output: + // + // Match found: False + #endregion + + Assert.False(isMatch); + } + } }