From 9e3fba03aedd6e09eb2b2d86c700c4f656345143 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Tue, 21 Jul 2026 20:36:57 +0000
Subject: [PATCH 1/2] Initial plan
From 249bb77c4da1ffa8ff17cb563de2d781aa88253d Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Tue, 21 Jul 2026 21:11:39 +0000
Subject: [PATCH 2/2] Backport missing Regex remarks and examples
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
---
.../System/Text/RegularExpressions/Regex.cs | 66 +++++++++++++++
.../tests/FunctionalTests/Regex.Examples.cs | 84 +++++++++++++++++++
2 files changed, 150 insertions(+)
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..4995c5f2e44e91 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,26 @@ protected Regex()
/// Initializes a new instance of the class for the specified regular expression.
///
/// The regular expression pattern to match.
+ /// constructor is equivalent to calling the
+ /// constructor with a value of
+ /// for the `options` argument.
+ ///
+ /// A object is immutable, which means that 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.
+ ///
+ /// ## Examples
+ ///
+ /// The following example uses this constructor to create a regular expression that matches words beginning with the letters "a" or "t".
+ ///
+ /// [!code-csharp[](../../../../tests/FunctionalTests/Regex.Examples.cs#RegexCtorString)]
+ /// ]]>
/// A regular expression parsing error occurred.
/// is .
public Regex([StringSyntax(StringSyntaxAttribute.Regex)] string pattern) :
@@ -95,6 +115,19 @@ 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.
+ /// object is immutable, which means that 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.
+ ///
+ /// ## Examples
+ ///
+ /// The following example uses this constructor to create a case-insensitive regular expression that matches words beginning with the letters "a" or "t".
+ ///
+ /// [!code-csharp[](../../../../tests/FunctionalTests/Regex.Examples.cs#RegexCtorStringOptions)]
+ /// ]]>
/// A regular expression parsing error occurred.
/// is .
///
@@ -115,6 +148,39 @@ public Regex([StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string
///
/// A time-out interval, or to indicate that the method should not time out.
///
+ /// object is immutable, which means that 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 `matchTimeout` 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 `matchTimeout` 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](https://learn.microsoft.com/dotnet/standard/base-types/best-practices-regex) and
+ /// [Backtracking in regular expressions](https://learn.microsoft.com/dotnet/standard/base-types/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.
+ ///
+ /// ## Examples
+ ///
+ /// The following example creates a object with a very short initial time-out and retries with a larger one if a timeout occurs.
+ ///
+ /// [!code-csharp[](../../../../tests/FunctionalTests/Regex.Examples.cs#RegexCtorStringOptionsMatchTimeout)]
+ /// ]]>
/// 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);
+ }
+ }
}