PowerShell parsing

406 Views Asked by At

I have the below input from a Git log:

Merge: d9335ae 7d12d50
Author: name\name <[email protected]>
Date:   Wed Oct 31 12:55:00 2018 -0500

id:202847 Merge branch 'release/2.6.0' into release/3.0.0

# Conflicts:
#   configuration/path/path

I need to parse the id:xxxxx and then pass that id into the target process API which returns the ID name.

I have a custom PowerShell function that accomplishes this.

What I need help with is combining this into one PowerShell function that does this. My regular expression to match the id is a simple id:\d+.

My desired outcome is the output to look like the below (the text after the ID is what's returned from the target process):

TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
2

There are 2 best solutions below

2
On BEST ANSWER

PowerShell has very powerful native regular expression support, and you can pretty easily retrieve the ID value from your Git commands like so. We begin by capturing the output of your git command (in my case, I pasted it into a variable, but you could also run $CommitMsg = git commit 202847 to capture the output into a variable another way :)

$r="Merge: d9335ae 7d12d50
Author: name\name <[email protected]>
Date:   Wed Oct 31 12:55:00 2018 -0500

id:202847 Merge branch 'release/2.6.0' into release/3.0.0

# Conflicts:
#   configuration/path/path"

Next, I use the PowerShell regular expression accelerator ([regex], PowerShell has lots of useful accelerators!) to look through the variable $r for matches for the regular expression pattern id:.....\w+, which looks for a string that starts with id: and has six characters after it, then a white space.

[regex]::Match($r, "id:(.....)\w+").Value
>id:202847

You can store that output in a variable like so:

$CommitID = [regex]::Match($r, "id:(.....)\w+").Value

and then embed that into your other command using string expansion, like this:

"TP $CommitID Professional Lines: 2,800,000 Policy Aggregate Limit update"
>TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update
2
On

To offer a more PowerShell-idiomatic alternative:

# Sample log text (multi-line string in the form of a here-string).
$logText = @'
Merge: d9335ae 7d12d50
Author: name\name <[email protected]>
Date:   Wed Oct 31 12:55:00 2018 -0500

id:202847 Merge branch 'release/2.6.0' into release/3.0.0

# Conflicts:
#   configuration/path/path
'@

# Extract the ID number alone, via a capture group `(...)`, using the
# -match regex operator and the automatic $Matches variable that reflects the 
# results.
# Entry 1 of $Matches contains the 1st (and here only) capture-group value.
# \b is used to make the regex more robust, by only matching at word boundaries.
# With the sample input, $id receives value '202847'
$id = if ($logText -match '\bid:(\d+)\b') { $Matches[1] }

# Note: If your input comes directly from a *file*, say 'commit.log', 
#       use the following command instead:
#
#  $id = (Select-String -list '\bid:(\d+)\b' commit.log).Matches.Groups[1].Value
#
# Alternatively, if reading the whole file into memory at once is acceptable,
# pass (Get-Content -Raw commit.log) instead of $logText to -match.

# Build the desired output string from the ID obtained and the API return value.
"TP Id:$id " + $returnValFromApi