PoshCode Archive  Artifact [d0af66cb7c]

Artifact d0af66cb7c656eee41843dfa7269834a841664a0395089bcbeaa0ffe038d5993:

  • File Get-Labels.ps1 — part of check-in [b1cac2c0cf] at 2018-06-10 13:37:23 on branch trunk — 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. (user: Joel Bennett size: 2341)

# 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: 4141
# x-archived: 2015-03-05T04:05:38
# x-published: 2015-05-02T17:37:00
#
#
function Get-Label {
    #.Synopsis
    #   Get labelled data using Regex
    #.Example
    #   openssl.exe 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)]
        [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}
                }
            }
        }
    }
}

# private static void ReportGroupsAndOutput( string text, Regex rgx )
# {
 
#     string[] groupNames = rgx.GetGroupNames();
 
#     Console.WriteLine("Groups: ({0}){1}", string.Join(") (", groupNames), System.Environment.NewLine);
 
#     MatchCollection mc = rgx.Matches(text);
 
#     foreach (Match m in mc)
 
#         if (m.Success)
#         {
#             Console.WriteLine("Match:");
 
#             foreach (string name in groupNames)
#                 Console.WriteLine("{0,10} : {1}", name, m.Groups[name]);
 
#             Console.WriteLine("{0}", System.Environment.NewLine);
 
#         }
# }