Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docfx.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"build": {
"markdownEngineName": "markdig",
"markdownEngineName": "markdig",
"markdownEngineProperties": {
"markdigExtensions": [
"abbreviations",
"definitionlists",
"tasklists",
"footnotes",
"diagrams"
]
},
"content": [
{
"files": ["api/**/*.md"]
Expand Down
3 changes: 3 additions & 0 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2251,6 +2251,9 @@ items:
- name: Overview
href: ../standard/base-types/regular-expressions.md
displayName: regular expressions
- name: Source generation
href: ../standard/base-types/regular-expression-source-generators.md
displayName: regular expressions,advanced,design concepts,source generators,algorithmic reduction,regex
- name: Language reference
items:
- name: Overview
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
400 changes: 400 additions & 0 deletions docs/standard/base-types/regular-expression-source-generators.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/standard/base-types/regular-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ helpviewer_keywords:
- "strings [.NET], regular expressions"
ms.assetid: 521b3f6d-f869-42e1-93e5-158c54a6895d
---

# .NET regular expressions

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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Text;
using System.Text.RegularExpressions;

static partial class Program
{
private static readonly Regex s_abcOrDefGeneratedRegex = AbcOrDefGeneratedRegex();

[GeneratedRegex(
pattern: "abc|def",
options: RegexOptions.IgnoreCase | RegexOptions.Compiled,
cultureName: "en-US")]
private static partial Regex AbcOrDefGeneratedRegex();

private static void EvaluateText(string text)
{
if (s_abcOrDefGeneratedRegex.IsMatch(text))
{
Console.WriteLine($"""
✅ "{text}" matches "{s_abcOrDefGeneratedRegex}" pattern.
""");
}
else
{
Console.WriteLine($"""
❌ "{text}" doesn't match "{s_abcOrDefGeneratedRegex}" pattern.
""");
}
}

private static void Main()
{
Console.OutputEncoding = Encoding.UTF8;

new List<string> { "Incubus", "Deftones", "Tool" }.ForEach(EvaluateText);

// Sample output:
// ❌ "Incubus" doesn't match "abc|def" pattern.
// ✅ "Deftones" matches "abc|def" pattern.
// ❌ "Tool" doesn't match "abc|def" pattern.

var abcOrDefRegex = new Regex(pattern: "abc|def", options: RegexOptions.IgnoreCase);
}
}

static file partial class Program
{
[GeneratedRegex(pattern: @"(a|bc)d")]
private static partial Regex ExampleRegex();

[GeneratedRegex(pattern: "[ab]*[bc]")]
private static partial Regex AnotherExampleRegex();

[GeneratedRegex(pattern: "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday")]
private static partial Regex DaysOfWeekRegex();

[GeneratedRegex(pattern: "")]
private static partial Regex BlankRegex();

[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})(\]?)$")]
private static partial Regex EmailRegex();

[GeneratedRegex(pattern: "(\\w)\\1")]
private static partial Regex WordWithBacktrackingRegex();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>