-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathValue.regex.ps1
126 lines (117 loc) · 4.3 KB
/
Value.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<#
.SYNOPSIS
Matches a YAML key
.DESCRIPTION
Matches a YAML key. The -Key can be customized.
.EXAMPLE
Get-Module Irregular |
Split-Path |
Get-ChildItem -Recurse -Force -Filter *.yml |
?<YAML_Key> -Key uses -IncludeInputObject |
Foreach-Object {
$_.InputObject |
?<YAML_Value> -Text $y -StartAt $_.Index -Count 1
}
#>
param(
# The key to match. By default, this is any key.
[string]
$Key = '[^\:]+'
)
[Regex]::new(
$(
<#?<
New-RegEx -Description "Matches a YAML key" -Modifier Multiline -StartAnchor LineStart -Comment 'A key starts off a line' |
New-RegEx -CharacterClass Whitespace -Min 0 -Comment 'Followed by whitespace' -Name Indent |
New-Regex -Name InList (
New-RegEx -LiteralCharacter '-' |
New-RegEx -CharacterClass Whitespace
) -Optional -Comment 'Followed by an optional list start'
>#>
@'
(?<=
# Matches a YAML key
(?m)^ # A key starts off a line
(?<Indent>\s){0,} # Followed by whitespace
(?<InList>\-\s)? # Followed by an optional list start
'@ +
[Environment]::NewLine +
$Key +
[Environment]::NewLine
<#?<
New-RegEx -LiteralCharacter ':' -Comment 'Followed by colon'
>#>
@'
\: # Followed by colon
)
'@
<#?<
New-RegEx -Atomic -Or @(
New-RegEx -Atomic -Or @(
New-RegEx -Name IsChild -Pattern (
New-RegEx -Before '[\r\n]{1,2}\k<Indent>\s{2}'
) -Optional |
New-RegEx -Name Value (
New-RegEx -NoCapture -Min 1 (
New-RegEx -Pattern '[\r\n]{1,2}' |
New-RegEx -Backreference Indent |
New-RegEx -Atomic -Or @(
New-RegEx -Pattern '\-\s'
New-RegEx -Pattern '\s{2}'
) |
New-RegEx -Pattern '[^\r\n]{1,}'
)
)
New-RegEx -Pattern "[\s-[\r\n]]" -Comment "Inline values start with a single space" |
New-Regex -Atomic -Or @(
New-RegEx -Name IsBlock -Pattern '\|(?>\+|\-)?(?=[\r\n]{1,2})' -Optional |
New-RegEx -Name Value (
New-RegEx -NoCapture -Min 1 (
New-RegEx -Pattern '[\r\n]{1,2}' |
New-RegEx -Backreference Indent |
New-RegEx -Atomic -Or @(
New-RegEx -Pattern '\-\s'
New-RegEx -Pattern '\s{2}'
) |
New-RegEx -Pattern '[^\r\n]{1,}'
)
)
New-RegEx -Name ValueInline (
New-RegEx -Not -LiteralCharacter "'`"" -CharacterClass NewLine, CarriageReturn -Repeat -Name Value -Comment "An unquoted value"
)
New-RegEx -LiteralCharacter "'" |
New-RegEx -Until (
New-RegEx -NotAfter "'" |
New-RegEx -LiteralCharacter "'"
) -Name Value |
New-RegEx -LiteralCharacter "'" -Comment "OR a single quoted value"
New-RegEx -LiteralCharacter '"' |
New-RegEx -Until (
New-RegEx -NotAfter '\\' |
New-RegEx -LiteralCharacter '"'
) -Name Value |
New-RegEx -LiteralCharacter '"' -Comment "OR a double quoted value"
)
)
)
>#>
@'
(?>
(?>
(?<IsChild>(?=[\r\n]{1,2}\k<Indent>\s{2}))?(?<Value>(?:[\r\n]{1,2}\k<Indent>(?>
\-\s |
\s{2})[^\r\n]{1,}){1,}) |
[\s-[\r\n]] # Inline values start with a single space
(?>
(?<IsBlock>\|(?>\+|\-)?(?=[\r\n]{1,2}))?(?<Value>(?:[\r\n]{1,2}\k<Indent>(?>
\-\s |
\s{2})[^\r\n]{1,}){1,}) |
(?<ValueInline>(?<Value>[^\n\r\'\"]+) # An unquoted value
) |
\'(?<Value>(?:.|\s){0,}?(?=\z|(?<!')\'))\' # OR a single quoted value
|
\"(?<Value>(?:.|\s){0,}?(?=\z|(?<!\\)\"))\" # OR a double quoted value
)))
'@
),'IgnoreCase,IgnorePatternWhitespace','00:00:01'
)