Find-NthIndexOf.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<#PSScriptInfo
 
.Version
    1.1
.Guid
    a4de76e8-d5c6-4592-8b04-324acbe17355
.Author
    Thomas Malkewitz @dotps1
.Tags
    Regex, Index
.ProjectUri
    https://github.com/dotps1/PSFunctions
.ReleaseNotes
    Move escaping the regex value from begin block to the process block to handle better pipeline support.
#>


<#
 
.Synopsis
    Finds the nth index of a char in a string.
.Description
    Finds the nth index of a char in a string, returns -1 if the char does not exist, or if nth is out of range.
.Inputs
    System.Int
    System.String
.Outputs
    System.Int
.Parameter Target
    System.String
    The string to evaluate.
.Parameter Value
    System.String
    The character to locate.
.Parameter Nth
    System.Int
    the occurrence of the character to find.
.Parameter IgnoreCase
    Preforms a case insensitive regex match.
.Example
    PS C:\> Find-NthIndexOf -Target "CN=me,OU=Users,DC=domain,DC=org" -Value "=" -Nth 2
    8
.Example
    PS C:\> ($dn = "CN=dotps1,OU=Users,DC=domain,DC=org").SubString((Find-NthIndexOf -Target $dn -Value "=" -Nth 2) - 2)
    OU=Users,DC=domain,DC=org
.Example
    PS C:\> Find-NthIndexOf -Target "Hello World." -Value "w" -IgnoreCase
    6
.Notes
    Returns -1 if the char does not exist, or if nth is out of range.
.Link
    http://stackoverflow.com/questions/186653/c-sharp-indexof-the-nth-occurrence-of-a-string
.Link
    https://dotps1.github.io
.Link
    https://www.powershellgallery.com/packages/Find-NthIndexOf
.Link
    https://grposh.github.io
 
#>



[CmdletBinding()]
[OutputType(
    [Int]
)]

param (
    [Parameter(
        Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true
    )]
    [String[]]
    $Target,

    [Parameter(
        Mandatory = $true,
        ValueFromPipelineByPropertyName = $true
    )]
    [ValidateLength(
        1,1
    )]
    [ValidateNotNullOrEmpty()]
    [String]
    $Value,

    [Parameter(
        ValueFromPipelineByPropertyName = $true
    )]
    [Int]
    $Nth = 1,

    [Parameter()]
    [Switch]
    $IgnoreCase
)

process {
    $Value = [Regex]::Escape(
        $Value
    )

    foreach ($targetValue in $Target) {
        $regex = "(($Value).*?){$Nth}" 
        if ($IgnoreCase.IsPresent) {
            $regex = $regex.Insert(
                0, "(?i)"
            )
        } 

        $match = [Regex]::Match(
            $targetValue,  $regex
        )

        if ($match.Success) {
            Write-Output -InputObject $match.Groups[2].Captures[$Nth - 1].Index
        } else {
            Write-Output -InputObject -1
        }
    }
}