Skip to content

Commit 5ddf794

Browse files
Ported as conceptual article from .NET dev blog: Regex enhancements with .NET 7 (dotnet#31587)
* Initial bits and raw port * Added to TOC * Fix syntax error * Minor updates and tweaks * Minor update * Updates, removals, clean up * Fix names and paths * Updates and fixes * More updates and clean up * Getting close * More fixes and updates, and adjusted headings * Finishing touches * A bit more clean up * Testing footnote extensions * Testing diagram extensions * Testing definition list support with markdig exts * Add another footnote and code updates * Ready for review * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * Address additional feedback * A bit of MD cleanup Co-authored-by: Genevieve Warren <[email protected]>
1 parent b414be4 commit 5ddf794

File tree

11 files changed

+488
-1
lines changed

11 files changed

+488
-1
lines changed

docfx.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
{
22
"build": {
3-
"markdownEngineName": "markdig",
3+
"markdownEngineName": "markdig",
4+
"markdownEngineProperties": {
5+
"markdigExtensions": [
6+
"abbreviations",
7+
"definitionlists",
8+
"tasklists",
9+
"footnotes",
10+
"diagrams"
11+
]
12+
},
413
"content": [
514
{
615
"files": ["api/**/*.md"]

docs/fundamentals/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,9 @@ items:
22532253
- name: Overview
22542254
href: ../standard/base-types/regular-expressions.md
22552255
displayName: regular expressions
2256+
- name: Source generation
2257+
href: ../standard/base-types/regular-expression-source-generators.md
2258+
displayName: regular expressions,advanced,design concepts,source generators,algorithmic reduction,regex
22562259
- name: Language reference
22572260
items:
22582261
- name: Overview
60.6 KB
Loading
60 KB
Loading
444 KB
Loading
353 KB
Loading
404 KB
Loading

docs/standard/base-types/regular-expression-source-generators.md

Lines changed: 400 additions & 0 deletions
Large diffs are not rendered by default.

docs/standard/base-types/regular-expressions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ helpviewer_keywords:
2323
- "strings [.NET], regular expressions"
2424
ms.assetid: 521b3f6d-f869-42e1-93e5-158c54a6895d
2525
---
26+
2627
# .NET regular expressions
2728

2829
Regular expressions provide a powerful, flexible, and efficient method for processing text. The extensive pattern-matching notation of regular expressions enables you to quickly parse large amounts of text to:
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Text;
2+
using System.Text.RegularExpressions;
3+
4+
static partial class Program
5+
{
6+
private static readonly Regex s_abcOrDefGeneratedRegex = AbcOrDefGeneratedRegex();
7+
8+
[GeneratedRegex(
9+
pattern: "abc|def",
10+
options: RegexOptions.IgnoreCase | RegexOptions.Compiled,
11+
cultureName: "en-US")]
12+
private static partial Regex AbcOrDefGeneratedRegex();
13+
14+
private static void EvaluateText(string text)
15+
{
16+
if (s_abcOrDefGeneratedRegex.IsMatch(text))
17+
{
18+
Console.WriteLine($"""
19+
✅ "{text}" matches "{s_abcOrDefGeneratedRegex}" pattern.
20+
""");
21+
}
22+
else
23+
{
24+
Console.WriteLine($"""
25+
❌ "{text}" doesn't match "{s_abcOrDefGeneratedRegex}" pattern.
26+
""");
27+
}
28+
}
29+
30+
private static void Main()
31+
{
32+
Console.OutputEncoding = Encoding.UTF8;
33+
34+
new List<string> { "Incubus", "Deftones", "Tool" }.ForEach(EvaluateText);
35+
36+
// Sample output:
37+
// ❌ "Incubus" doesn't match "abc|def" pattern.
38+
// ✅ "Deftones" matches "abc|def" pattern.
39+
// ❌ "Tool" doesn't match "abc|def" pattern.
40+
41+
var abcOrDefRegex = new Regex(pattern: "abc|def", options: RegexOptions.IgnoreCase);
42+
}
43+
}
44+
45+
static file partial class Program
46+
{
47+
[GeneratedRegex(pattern: @"(a|bc)d")]
48+
private static partial Regex ExampleRegex();
49+
50+
[GeneratedRegex(pattern: "[ab]*[bc]")]
51+
private static partial Regex AnotherExampleRegex();
52+
53+
[GeneratedRegex(pattern: "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday")]
54+
private static partial Regex DaysOfWeekRegex();
55+
56+
[GeneratedRegex(pattern: "")]
57+
private static partial Regex BlankRegex();
58+
59+
[GeneratedRegex(pattern: @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")]
60+
private static partial Regex EmailRegex();
61+
62+
[GeneratedRegex(pattern: "(\\w)\\1")]
63+
private static partial Regex WordWithBacktrackingRegex();
64+
}

0 commit comments

Comments
 (0)