# encoding: ascii
# api: powershell
# title: Get-Labels
# description: Pulls label-value pairs from text. Note that this version is REALLY, REALLY optimistic, and assumes that your label-value pairs are each, always, on their own line.
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Get-Label
# x-poshcode-id: 4143
# x-derived-from-id: 4144
# x-archived: 2015-03-05T02:10:39
# x-published: 2015-05-02T17:47:00
#
#
function Get-Label {
    #.Synopsis
    #   Get labelled data using Regex
    #.Example
    #   openssl crl -in .\CSC3-2010.crl -inform der -text | Get-Label "Serial Number:" "Revocation Date:"
    param(
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [AllowEmptyString()]
        [string]$data,

        [Parameter(ValueFromRemainingArguments=$true, Position = 1)]
        [string[]]$labels = ("Serial Number:","Revocation Date:")
    )
    begin {
        [string[]]$FullData = $data
    }
    process {
        [string[]]$FullData += $data
    }

    end {
        $data = $FullData -join "`n"

        $names = $labels -replace "\s+" -replace "\W"

        $re = "(?m)" + (@(
            for($l=0; $l -lt $labels.Count; $l++) {
                $label = $labels[$l]
                $name = $names[$l]
                "$label\s*(?<$name>.*)\s*`$"
            }) -join "|")
        write-host $re
        $re = [Regex]$re

        $matches = $re.Matches($data)    
        foreach($match in $matches | where Success) {
            foreach($name in $names) {
                if($match.Groups[$name].Value) {
                    @{$name = $match.Groups[$name].Value}
                }
            }
        }
    }
}
