RegexStringLiterals 1.0.0.1

dotnet add package RegexStringLiterals --version 1.0.0.1                
NuGet\Install-Package RegexStringLiterals -Version 1.0.0.1                
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="RegexStringLiterals" Version="1.0.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RegexStringLiterals --version 1.0.0.1                
#r "nuget: RegexStringLiterals, 1.0.0.1"                
#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.
// Install RegexStringLiterals as a Cake Addin
#addin nuget:?package=RegexStringLiterals&version=1.0.0.1

// Install RegexStringLiterals as a Cake Tool
#tool nuget:?package=RegexStringLiterals&version=1.0.0.1                

Regex String Literal Analyzer

A Roslyn-based analyzer that validates string literal assignments against RegularExpression attribute constraints at compile time. This helps you catch configuration errors early�before runtime�improving code quality and reducing potential bugs.


Overview

The Regex String Literal Analyzer inspects code for properties, fields, or assignments annotated with the [RegularExpression(...)] attribute. It verifies that the string literal assigned matches the regular expression provided in the attribute. When a mismatch is found, the analyzer reports a diagnostic (warning by default, configurable via .editorconfig) so you can fix the issue immediately.

Key Features

  • Compile-Time Validation: Ensures that string literals assigned to properties decorated with [RegularExpression] actually conform to the specified regex pattern.
  • Diagnostic Reporting: Flags invalid assignments with a diagnostic message (e.g., REGEX001) that indicates the expected pattern.
  • Code Fixes: Provides quick fixes to help correct the invalid assignment.
  • Multiple Distribution Options: Available as both a VSIX extension for Visual Studio and as a NuGet package for seamless integration into your projects.

Installation

Via NuGet

Install the analyzer as a NuGet package so that it automatically runs during your build:

Using Package Manager Console:

Install-Package RegexStringLiterals.Analyzer

Using .NET CLI:

dotnet add package RegexStringLiterals.Analyzer

Via VSIX

If you prefer to use a Visual Studio extension:

  1. Download the VSIX package.
  2. Double-click the .vsix file and follow the installation prompts.
  3. Restart Visual Studio if necessary.
  4. Verify installation by opening Extensions > Manage Extensions and confirming that "Regex String Literal Analyzer" appears in the list.

For VS Code

Per Project(nuget):
Prerequisites
  • .NET SDK
  • C# Extension for Visual Studio Code

To use the analyzer in VS Code:

  1. Open your .NET project folder in VS Code.
  2. Install the analyzer NuGet package:
dotnet add package RegexStringLiterals.Analyzer
  1. Restore the project dependencies:
dotnet restore
  1. The analyzer will automatically run in the background. See usage instructions below for validation details.
Globally(extension):

Coming soon!


Usage

Once installed, the analyzer works automatically. For example, consider the following code snippet:

using System.ComponentModel.DataAnnotations;

public class TestClass {
    [RegularExpression("red|green|blue")]
    public string Color { get; set; } = "yellow"; // "yellow" does not match the pattern and will trigger a diagnostic.
}

Since "yellow" does not match the regex "red|green|blue", the analyzer will flag this assignment with a diagnostic message like:

REGEX001: Value 'yellow' does not match the pattern: red|green|blue.

If code fixes are available, you can trigger them via Visual Studio�s Quick Actions (usually by clicking the lightbulb icon).

The analyzer can flag invalid inputs for any regex strings but will only provide Qucik Action fixes for explicitly defined strings(such as the above example).


Configuration

By default, the analyzer reports a warning when a mismatch is found. You can customize the severity (or even suppress the diagnostic) using an .editorconfig file in your project:

# Treat RegexStringLiteral diagnostics as errors
dotnet_diagnostic.REGEX001.severity = error

# Or to disable the analyzer:
dotnet_diagnostic.REGEX001.severity = none

Additionally, you may include other configuration settings as the project evolves.


Contributing

Contributions are welcome! If you find a bug or have suggestions for improvements, please open an issue or submit a pull request on GitHub.

Getting Started with Development

  1. Fork the Repository: Create your own fork on GitHub.
  2. Clone Your Fork: Clone the repository to your local machine.
  3. Open the Solution: Open the solution in Visual Studio.
  4. Build and Test: Build the project and run the automated tests to ensure everything works as expected.
  5. Submit Your Changes: Once your changes are ready, submit a pull request for review.

License

This project is licensed under the MIT License. Please see the LICENSE file for details.


Changelog

See the CHANGELOG.md file for a detailed history of changes.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has 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.1 66 2/5/2025