Skip to content

GeneratedRegex attribute #95

@gregsdennis

Description

@gregsdennis

Description (optional)

Support for [GeneratedRegex] attribute. MS Docs

Rationale

This is the new .Net 8 way of having static readonly regexes in code:

[GeneratedRegex(@"^by\(\s*(?<var>[a-zA-Z_][a-zA-Z0-9_]*)\s*\)")]
private static partial Regex MyRegex();
private static readonly Regex _byForm = MyRegex();

Prior to .Net 8, this is required:

private static readonly Regex _byForm = new(@"^by\(\s*(?<var>[a-zA-Z_][a-zA-Z0-9_]*)\s*\)");

which is arguably cleaner, but it means that regex table lookups are performed at runtime instead of at compile time. (I expect my usage isn't an ideal example.)

Proposed API

No proposal

Drawbacks

None

Alternatives

None

Other thoughts

None.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions