Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Last active July 25, 2023 23:12
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IISResetMe/654b302383a687bd92faa8c8c3ab28fa to your computer and use it in GitHub Desktop.
Save IISResetMe/654b302383a687bd92faa8c8c3ab28fa to your computer and use it in GitHub Desktop.
Quick and dirty regex-based text-to-object parsing using named expressions groups and $Matches
function ConvertTo-Object {
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[AllowEmptyString()]
[string[]]$InputString,
[Parameter(Mandatory=$true,ValueFromRemainingArguments=$true)]
[string[]]$Pattern
)
process{
foreach($string in $InputString |?{$_}){
foreach($p in $Pattern){
if($string -match $p){
if($PropertyNames = $Matches.Keys |Where-Object {$_ -is [string]}){
$Properties = $PropertyNames |ForEach-Object -Begin {$t = @{}} -Process {$t[$_] = $Matches[$_]} -End {$t}
[PSCustomObject]$Properties
}
break
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment