GuardRegex 1.0.0

dotnet add package GuardRegex --version 1.0.0
                    
NuGet\Install-Package GuardRegex -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GuardRegex" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GuardRegex" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="GuardRegex" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GuardRegex --version 1.0.0
                    
#r "nuget: GuardRegex, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GuardRegex@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GuardRegex&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=GuardRegex&version=1.0.0
                    
Install as a Cake Tool

GuardRegex

License: MIT

Overview

GuardRegex is a wrapper utility for .NET Regex.

It performs lightweight input validation and enforces a timeout setting when invoking .NET Regex.

  • Regex metacharacter escaping support
  • Null input validation
  • Excessively long input validation
  • Prevention of executions without an explicit timeout

GuardRegex relies on the error handling provided by .NET Regex and transparently propagates exceptions raised by .NET Regex.


Intended Audience

Developers who use .NET Regex on a regular basis.


Features

RegexEscaper

An extended escaper/unescaper for System.Text.RegularExpressions.Regex.Escape and Unescape.

Features:

  • Conservatively escapes and unescapes a broad set of special symbols used in .NET Regex syntax
  • Restores escaped strings through unescaping
  • Unescape(Escape(x)) == x
  • Escape(Unescape(x)) != x

Example:

string escaped = RegexEscaper.Escape("(sample)");
Console.WriteLine(escaped);

string restored = RegexEscaper.Unescape(escaped);
Console.WriteLine(restored);
GuardRegex

A protective wrapper around the invocation boundary of System.Text.RegularExpressions.Regex.

Features:

  • Null validation
  • Maximum input length validation
  • Default timeout enforcement
  • Regex API wrapping

Example:

var regex = new GuardRegex(@"\d+", timeout: TimeSpan.FromSeconds(2));
bool matched = regex.IsMatch("123");

Exceptions and Responsibility Boundaries

GuardRegex does not replace the regular expression engine.

The following behaviors are delegated to System.Text.RegularExpressions.Regex:

  • Regular expression syntax errors
  • Regular expression engine behavior
  • Matching algorithms
  • Exceptions raised due to timeout
  • Behaviors dependent on the .NET Runtime Regex implementation

Design Principles

GuardRegex emphasizes the Single Responsibility Principle.

  • RegexEscaper is responsible for string transformation.
  • GuardRegex is responsible for protecting the Regex invocation boundary.

Features beyond these responsibilities are intentionally excluded.


License

License: MIT

  • This project is distributed under the MIT License.

Disclaimer

  • This software is provided as-is, and all responsibility for its use belongs to the user.
  • The developer's employer, affiliation, or contractual status does not affect the terms of use or responsibilities associated with this software.

Contribution Policy

  • This project is primarily intended for personal technical research and publication. At present, external contributions via Issues or Pull Requests are not accepted. Thank you for your understanding.
  • Bug reports related to implemented functionality are welcome.

Author

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 106 8/11/2026

Version 1.0.0

     Initial stable release of GuardRegex.

     - RegexEscaper: extended escaping and unescaping support for regex metacharacters.
     - GuardRegex: protective wrapper for Regex execution with null checks, maximum input length validation, and timeout enforcement.
     - Transparent propagation of exceptions from System.Text.RegularExpressions.Regex.
     - MIT License.