Add ToRegex method to WildcardPattern class#26515
Add ToRegex method to WildcardPattern class#26515iSazonov merged 10 commits intoPowerShell:masterfrom
Conversation
- 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
There was a problem hiding this comment.
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.
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
Address review feedback to check exactly expected strings in all tests instead of using -Match assertions.
Add tests for trailing .*$ and both-end optimization
There was a problem hiding this comment.
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.
|
@daxian-dbw @SeeminglyScience Looking as the new method could be used should it return |
| /// </example> | ||
| public string ToRegex() | ||
| { | ||
| return PatternConvertedToRegex; |
There was a problem hiding this comment.
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.
|
@yotsuda I think the method should return Regex. Could you please make the change? Already added tests are good in the case too. |
|
@iSazonov @MatejKafka Thanks for the feedback! I've updated Changes (ce1dcf3):
|
There was a problem hiding this comment.
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.
| private Predicate<string> _isMatch; | ||
|
|
||
| // cached regex for ToRegex() method | ||
| private Regex _regex; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thanks for the review! I've removed the cache as you suggested. (793b74e)
PR Summary
Add public
ToRegex()method toWildcardPatternclass 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 theWildcardPatternclass.Usage Example
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).→\.)*returns empty string)PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerDescription
Implementation
Added a public
ToRegex()method to theWildcardPatternclass that:PatternConvertedToRegexpropertyRegexobject) for maximum flexibilityWildcardOptions(IgnoreCase, CultureInvariant, Compiled)Files Changed
src/System.Management.Automation/engine/regex.cs- Added publicToRegex()methodtest/powershell/engine/WildcardPattern.Tests.ps1- Added comprehensive test coverage (13 tests)Testing
Added Pester tests covering:
*,?,[])WildcardPattern.IsMatch()behavior*)All 13 tests pass locally.
Additional Notes