Skip to content

Make RegexParseException public #372

@OskarKlintrot

Description

@OskarKlintrot

I'm trying to validate a regex pattern but I can't catch System.Text.RegularExpressions.RegexParseException since it's internal.

I can solve it by using pattern matching but it feels like a bodge:

private static bool IsValidRegex(string pattern)
{
    if (string.IsNullOrEmpty(pattern))
    {
        return false;
    }

    try
    {
        Regex.Match(string.Empty, pattern);
    }
    catch (Exception ex)
        when (ex is ArgumentException)
    {
        return false;
    }

    return true;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions