Skip to content

Add WildcardPattern ToRegex method #19992

@ThomasNieto

Description

@ThomasNieto

Summary of the new feature / enhancement

As a user I want to be able to convert a user provided PowerShell wildcard pattern into a regex pattern for use in programs that accept regex.

Proposed technical implementation details (optional)

It looks like there is already a converter for this but just needs to be made public.

internal string PatternConvertedToRegex
{
get
{
var patternRegex = WildcardPatternToRegexParser.Parse(this);
return patternRegex.ToString();
}
}

/// <summary>
/// Parses a <paramref name="wildcardPattern"/> into a <see cref="Regex"/>
/// </summary>
/// <param name="wildcardPattern">Wildcard pattern to parse.</param>
/// <returns>Regular expression equivalent to <paramref name="wildcardPattern"/></returns>
public static Regex Parse(WildcardPattern wildcardPattern)
{
WildcardPatternToRegexParser parser = new WildcardPatternToRegexParser();
WildcardPatternParser.Parse(wildcardPattern, parser);
try
{
return ParserOps.NewRegex(parser._regexPattern.ToString(), parser._regexOptions);
}
catch (ArgumentException)
{
throw WildcardPatternParser.NewWildcardPatternException(wildcardPattern.Pattern);
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issueIssue-Enhancementthe issue is more of a feature request than a bugUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions