-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathListItem.regex.ps1
104 lines (99 loc) · 3.24 KB
/
ListItem.regex.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
<#
.Synopsis
Matches a JSON list item
.Description
Matches a JSON list item. If no -ListIndex is provided, will match all items in the list
#>
param(
[int]
$ListIndex = -1
)
$jsonValue = @'
\s{0,} # Match preceeding whitespace
(?> # A JSON value can be:
(?<IsTrue>true) # 'true'
| # OR
(?<IsFalse>false) # 'false'
| # OR
(?<IsNull>null) # 'null'
| # OR
(?<Object> # an object, which is
\{ # An open brace
(?> # Followed by...
[^\{\}]+| # any number of non-brace character OR
\{(?<Depth>)| # an open brace (in which case increment depth) OR
\}(?<-Depth>) # a closed brace (in which case decrement depth)
)*(?(Depth)(?!)) # until depth is 0.
\} # followed by a closing brace
)
| # OR
(?<List> # a list, which is
\[ # An open bracket
(?> # Followed by...
[^\[\]]+| # any number of non-bracket character OR
\[(?<Depth>)| # an open bracket (in which case increment depth) OR
\](?<-Depth>) # a closed bracket (in which case decrement depth)
)*(?(Depth)(?!)) # until depth is 0.
\] # followed by a closing bracket
)
| # OR
(?<String> # A string, which is
" # an open quote
.*? # followed by anything
(?=(?<!\\)" # until the closing quote
)
| # OR
(?<Number> # A number, which
(?<Decimals>
(?<IsNegative>\-)? # It might be start with a -
(?:(?> # Then it can be either:
(?<Characteristic>\d+) # One or more digits (the Characteristic)
(?:\.(?<Mantissa>\d+)){0,1} # followed by a period and one or more digits (the Mantissa)
| # Or it can be
(?:\.(?<Mantissa>\d+)) # just a Mantissa
))
(?: # Optionally, there can also be an exponent
E # which is the letter 'e'
(?<Exponent>[+-]\d+) # followed by + or -, followed by digits.
)?
)
)
)
)
\s{0,} # Optionally match following whitespace
'@
$listItemParams = @{} + $PSBoundParameters
if (-not $listItemParams.ContainsKey('ListIndex')) {
@"
(?>
\[\s{1,}\] # An open bracket
|
\[
(?:
(?<ListItem>
$jsonValue
)
(?:,)?
){1,}
\]
)
"@
} elseif ($ListIndex -ge 0) {
$findListIndex = @"
(?>
\[
$(if ($ListIndex -ge 1) { @"
(?:
(?:$jsonValue)
(?:,)?
){$listIndex}
(?:,)?
"@
})
(?<ListItem>
$jsonValue
)
)
"@
$findListIndex
}