PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

Add ToRegex method to WildcardPattern class

Open yotsuda opened this issue 1 month ago • 3 comments

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

yotsuda avatar Nov 23 '25 11:11 yotsuda

@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.

iSazonov avatar Dec 04 '25 03:12 iSazonov

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

iSazonov avatar Dec 06 '25 04:12 iSazonov

@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 (ce1dcf368):

  • 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

yotsuda avatar Dec 06 '25 06:12 yotsuda