Skip to content

Add ToRegex method to WildcardPattern class#26515

Merged
iSazonov merged 10 commits intoPowerShell:masterfrom
yotsuda:feature/issue-19992-wildcard-toregex
Dec 10, 2025
Merged

Add ToRegex method to WildcardPattern class#26515
iSazonov merged 10 commits intoPowerShell:masterfrom
yotsuda:feature/issue-19992-wildcard-toregex

Conversation

@yotsuda
Copy link
Copy Markdown
Contributor

@yotsuda yotsuda commented Nov 23, 2025

PR Summary

Add public ToRegex() method to WildcardPattern class for converting PowerShell wildcard patterns to regular expressions.

PR Context

Resolves #19992

PowerShell users often need to convert wildcard patterns to regular expressions for use in programs that accept regex. While the internal implementation already exists, it was not accessible through a public API.

This PR exposes the conversion functionality by adding a public ToRegex() method to the WildcardPattern class.

Usage Example

$pattern = [System.Management.Automation.WildcardPattern]::new("*.txt")
$regex = $pattern.ToRegex()
$regexObj = [regex]::new($regex)
$regexObj.IsMatch("file.txt")  # True

Conversion Rules

  • * (asterisk) converts to .* in regex (matches zero or more characters)
  • ? (question mark) converts to . (matches exactly one character)
  • [abc], [a-z] (bracket expressions) preserve their regex meaning (character sets and ranges)
  • Literal characters are escaped as needed (e.g., .\.)
  • For backward compatibility, some optimizations are applied (e.g., standalone * returns empty string)

PR Checklist

Description

Implementation

Added a public ToRegex() method to the WildcardPattern class that:

  • Leverages the existing internal PatternConvertedToRegex property
  • Returns a string (not Regex object) for maximum flexibility
  • Respects all WildcardOptions (IgnoreCase, CultureInvariant, Compiled)
  • Includes comprehensive XML documentation with examples

Files Changed

  • src/System.Management.Automation/engine/regex.cs - Added public ToRegex() method
  • test/powershell/engine/WildcardPattern.Tests.ps1 - Added comprehensive test coverage (13 tests)

Testing

Added Pester tests covering:

  • Basic wildcard conversions (*, ?, [])
  • WildcardOptions handling (IgnoreCase, CultureInvariant)
  • Consistency with WildcardPattern.IsMatch() behavior
  • Edge cases (empty pattern, escaped characters, standalone *)
  • Complex patterns (multiple wildcards, character ranges)

All 13 tests pass locally.

Additional Notes

  • Pure addition - no breaking changes
  • Uses existing optimized conversion logic
  • Enables interoperability between PowerShell wildcards and regex-based tools

- Add public ToRegex() method to convert wildcard patterns to regex
- Leverage existing internal PatternConvertedToRegex property
- Include comprehensive XML documentation with examples
- Add 13 Pester tests covering various scenarios
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 24, 2025
@iSazonov iSazonov requested a review from Copilot November 24, 2025 05:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a public ToRegex() method to the WildcardPattern class, exposing existing internal wildcard-to-regex conversion functionality. This enables PowerShell users to convert wildcard patterns to regular expressions for use with regex-based tools and APIs.

Key changes:

  • Added public ToRegex() method that returns the regex string representation of the wildcard pattern
  • Included comprehensive XML documentation with conversion rules and usage examples
  • Added 13 Pester tests covering basic conversions, edge cases, wildcard options, and pattern matching consistency

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/System.Management.Automation/engine/regex.cs Added public ToRegex() method with XML documentation to expose internal pattern conversion
test/powershell/engine/WildcardPattern.Tests.ps1 New test file with comprehensive test coverage for the ToRegex() method including basic conversions, options, edge cases, and complex patterns

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/powershell/engine/WildcardPattern.Tests.ps1 Outdated
Comment thread src/System.Management.Automation/engine/regex.cs Outdated
Comment thread test/powershell/engine/WildcardPattern.Tests.ps1 Outdated
Comment thread test/powershell/engine/WildcardPattern.Tests.ps1 Outdated
yotsuda and others added 2 commits November 24, 2025 15:52
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Address Copilot review feedback:
- Use -BeExactly instead of -Match for precise regex validation
- Fix question mark test: ? converts to . (single char), not .*
- Fix escaped wildcard test: use single quotes to preserve backtick
- Add clarifying comments for test intentions
Comment thread test/powershell/engine/WildcardPattern.Tests.ps1 Outdated
Address review feedback to check exactly expected strings
in all tests instead of using -Match assertions.
Comment thread test/powershell/engine/WildcardPattern.Tests.ps1 Outdated
Add tests for trailing .*$ and both-end optimization
Comment thread src/System.Management.Automation/engine/regex.cs Outdated
Comment thread test/powershell/engine/WildcardPattern.Tests.ps1 Outdated
Comment thread test/powershell/engine/WildcardPattern.Tests.ps1 Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Dec 3, 2025
@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Dec 4, 2025

@daxian-dbw @SeeminglyScience Looking as the new method could be used should it return Regex type instead of string? I'd expect users want exactly get and use Regex and they will be forced to convert the string back to Regex. They can always call ToString() method themselves if necessary, for example, for debugging.

@microsoft-github-policy-service microsoft-github-policy-service Bot removed the Review - Needed The PR is being reviewed label Dec 4, 2025
/// </example>
public string ToRegex()
{
return PatternConvertedToRegex;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd somewhat expect a method called ToRegex() to return a Regex, not a string. Is there a good set of flags to set on Regex to reliably match what the pattern does? If so, I think it would be cleaner to return a pre-configured Regex instance so that the user is guaranteed to get exactly the same behavior.

@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Dec 6, 2025

@yotsuda I think the method should return Regex. Could you please make the change? Already added tests are good in the case too.

@yotsuda
Copy link
Copy Markdown
Contributor Author

yotsuda commented Dec 6, 2025

@iSazonov @MatejKafka Thanks for the feedback!

I've updated ToRegex() to return Regex instead of string. The implementation now caches the Regex instance for subsequent calls.

Changes (ce1dcf3):

  • ToRegex() returns Regex directly
  • Added _regex field to cache the instance
  • Removed the internal PatternConvertedToRegex property (was only used by ToRegex())
  • Added a test to verify that calling ToRegex() multiple times returns the same instance

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/powershell/engine/WildcardPattern.Tests.ps1
Comment thread test/powershell/engine/WildcardPattern.Tests.ps1
Comment thread src/System.Management.Automation/engine/regex.cs Outdated
Comment thread test/powershell/engine/WildcardPattern.Tests.ps1 Outdated
private Predicate<string> _isMatch;

// cached regex for ToRegex() method
private Regex _regex;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need the cache? For example ToWql() doesn't use cache.
I believe the cache is needed only for multiple retrieves in high performance scenarios, but it is not our case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! I've removed the cache as you suggested. (793b74e)

@iSazonov iSazonov self-assigned this Dec 10, 2025
@iSazonov iSazonov merged commit 7f92d58 into PowerShell:master Dec 10, 2025
44 of 46 checks passed
@yotsuda yotsuda deleted the feature/issue-19992-wildcard-toregex branch December 10, 2025 21:46
SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
kilasuit pushed a commit to kilasuit/PowerShell that referenced this pull request Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add WildcardPattern ToRegex method

4 participants