0

I noticed that dotnet is missing a lot of the javascript methods related to regular expression with the string class, for instince string.match().

Is there a class extension for c#/powershell that implements these missing method of the string class so that it more closely matches javascript?

For example in powershell I can add the method RegexCount() to the dotnet string class as follows:

$proto_RegexCount = @{
    Force      = $true
    TypeName   = "System.String"
    MemberName = "RegexCount"
    MemberType = [System.Management.Automation.PSMemberTypes]::ScriptMethod
    Value = {
        param([regex]$Regex)
        $Regex.Matches($this).Count
    }
}

Update-TypeData @proto_RegexCount
Bill Moore
  • 165
  • 4
  • 1
    `Regex.Match`?? You are welcome to add an extension method `static Match(this string input, string pattern) => Regex.Match(input, pattern);` although I don't know how that shows up in Powershell – Charlieface May 22 '22 at 17:37
  • 2
    To provide some context: You took the PowerShell code from [this answer](https://stackoverflow.com/a/72332293/45375), modifying only incidental aspects of the invocation syntax. You needed help with your [first attempt](https://stackoverflow.com/q/72337552/45375) at the alternative invocation syntax (I'm assuming that was you - it is a separate account, but has the same display name), and received it via [this answer](https://stackoverflow.com/a/72338012/45375). Without acknowledging the latter, you then used the solution as part of this question. – mklement0 May 22 '22 at 18:02
  • 2
    @Charlieface Unfortunately C# extension methods don't work in PowerShell. `Update-TypeData` is the only way to provide something similar. – zett42 May 22 '22 at 19:14
  • 2
    https://csharp-extension.com/en/method/1002168/string-matches – zett42 May 22 '22 at 20:36
  • 1
    [This answer](https://stackoverflow.com/a/65781830/7571258) shows how you can use C# extension with `Add-Type`. – zett42 May 22 '22 at 20:37

0 Answers0