Skip to content

[API Proposal]: [RegexGenerator(...)] attribute #58880

Description

@stephentoub

Background and motivation

Attribute to trigger regex source generation:
#44676

API Proposal

namespace System.Text.RegularExpressions
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public sealed class RegexGeneratorAttribute : Attribute
    {
        // Same three constructors as on Regex, except the timeout is an int (ms) instead of a TimeSpan
        public RegexGeneratorAttribute(string pattern);
        public RegexGeneratorAttribute(string pattern, RegexOptions options);
        public RegexGeneratorAttribute(string pattern, RegexOptions options, int matchTimeout);

        public string Pattern { get; }
        public RegexOptions Options { get; }
        public int MatchTimeout { get; }
    }
}

Alternate names:

  • RegexAttribute
  • GeneratedRegexAttribute
  • RegexSourceGeneratorAttribute
  • SourceGeneratedRegexAttribute
  • ...

(Note there's already a System.ComponentModel.DataAnnotations.RegularExpressionAttribute that's very different.)

API Usage

[RegexGenerator(@"\b[t]\w+", RegexOptions.IgnoreCase | RegexOptions.InvariantCulture)]
private static partial Regex GetRegexToFindWordsBeginningWithT();
...
Regex r = GetRegexToFindWordsBeginningWithT();
foreach (Match m in r.Matches(input))
{
    ...
}

Risks

n/a

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions