forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Add-CapturesToMatches.ps1
56 lines (47 loc) · 1.85 KB
/
Add-CapturesToMatches.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
.SYNOPSIS
Adds named capture group values as note properties to Select-String MatchInfo objects.
.DESCRIPTION
Navigating the .NET Regex Group and Capture collections to find a capture group can be
a hassle in some versions of PowerShell.
To simplifies this process, this script just adds simple properties to the MatchInfo objects.
.INPUTS
Microsoft.PowerShell.Commands.MatchInfo, output from Select-String that used a pattern
with named capture groups.
.OUTPUTS
Microsoft.PowerShell.Commands.MatchInfo with additional note properties for each named
capture group.
.FUNCTIONALITY
Search and replace
.LINK
Add-Member
.EXAMPLE
Select-String '^(?<Name>.*?\b)\s*(?<Email>\S+@\S+)$' addrbook.txt |Add-CapturesToMatches.ps1 |select Name,Email,Filename
Name Email Filename
---- ----- --------
Arthur Dent adent@example.org addrbook.txt
Tricia McMillan trillian@example.com addrbook.txt
#>
#Requires -Version 3
[CmdletBinding()][OutputType([Microsoft.PowerShell.Commands.MatchInfo])] Param(
# The MatchInfo output from Select-String to augment with named capture group values.
[Parameter(Position=0,ValueFromRemainingArguments=$true,ValueFromPipeline=$true)]
[Alias('InputObject')][Microsoft.PowerShell.Commands.MatchInfo]$MatchInfo
)
Process
{
if($PSVersionTable.PSEdition -eq 'Desktop' -and $PSVersionTable.CLRVersion -lt [version]4.7)
{ # old CLR is really tedious to get group names
[regex]$regex = $MatchInfo.Pattern
$regex.GetGroupNames() |
Where-Object {$_ -Match '\D'} |
ForEach-Object {Add-Member -InputObject $MatchInfo $_ $MatchInfo.Matches.Groups[$regex.GroupNumberFromName($_)].Value}
}
else
{
$MatchInfo.Matches.Groups |
Where-Object Name -Match '\D' |
ForEach-Object {Add-Member -InputObject $MatchInfo $_.Name $_.Value}
}
$MatchInfo
}