Postcard from the bowels of the regex beast!

I want to derive some exception classes and pass in a message that comes from the relevant value of ErrorCategory.

ErrorCategory is an enum with 32 values that are no-whitespace strings in Pascal case:

[Enum]::GetValues(
    [System.Management.Automation.ErrorCategory])
NotSpecified
OpenError
CloseError
DeviceError
DeadlockDetected
# ...etc

That [System.Enum]::GetValues() method works on any enum, e.g. [System.DayOfWeek]

I don’t want to text-edit them all myself, we have computers for that.

Step 1, split them:

[System.Enum]::GetValues(
    [System.Management.Automation.ErrorCategory]) |
    select -First 1 | foreach {
        [regex]::Matches(
            $_,
            '[A-Z][a-z]*'
        ).Value
    }
Not
Specified

Does anyone else select only the first item while work is in progress? Saves some scrolling. On which note, sorry about the awkward spacing, this blog theme makes horizontal space very precious.

Complete snippet:

[System.Enum]::GetValues(
    [System.Management.Automation.ErrorCategory]) |
    foreach {
        $Words = [regex]::Matches(
            $_,
            '[A-Z][a-z]*'
        ).Value

        $Words = @($Words[0]) + ($Words | select -Skip 1 | %{$_.ToLower()})
        $Words -join ' '
    }
Not specified
Open error
Close error
Device error
Deadlock detected
# ...etc

My love/hate relationship with regex continues.