Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WildcardPattern ToRegex method #19992

Open
ThomasNieto opened this issue Jul 17, 2023 · 3 comments
Open

Add WildcardPattern ToRegex method #19992

ThomasNieto opened this issue Jul 17, 2023 · 3 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors WG-Engine core PowerShell engine, interpreter, and runtime

Comments

@ThomasNieto
Copy link
Contributor

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);
}
}

@ThomasNieto ThomasNieto added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels Jul 17, 2023
@daxian-dbw daxian-dbw added the WG-Engine core PowerShell engine, interpreter, and runtime label Jul 21, 2023
@santisq
Copy link

santisq commented Jul 21, 2023

This would be great to have as a public API I think, for the time being, with ClassExplorer this can be obtained very easily:

$wp = [WildcardPattern]::new('foo*bar*.ext')
$member = $wp | Find-Member -Name PatternConvertedToRegex -Force
$member.GetValue($wp)

# Outputs: ^foo.*bar.*\.ext$

@SeeminglyScience
Copy link
Collaborator

The Engine WG reviewed this, we agree that the method should be made public but a PR to make this change should include tests to verify that it works as expected.

@SeeminglyScience SeeminglyScience added Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels Nov 13, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label May 12, 2024
@ThomasNieto
Copy link
Contributor Author

@SeeminglyScience why is this being flagged for closure even though it has up for grabs?

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Resolution-No Activity Issue has had no activity for 6 months or more label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

No branches or pull requests

4 participants