-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathVariable.regex.ps1
61 lines (56 loc) · 1.93 KB
/
Variable.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
<#
.Synopsis
Matches a REST variable
.Description
Matches variables within a RESTful URL. Variables can take several forms:
#>
param(
# The variable formats.
[ValidateSet('Braces', 'Brackets','Tags','TickDollar','ColonStart')]
[string[]]
$VariableFormat = @('Braces', 'Brackets', 'Tags', 'TickDollar', 'ColonStart'),
[switch]
$NotInQuery
)
process {
$varForms =
foreach ($vf in $VariableFormat) {
if ($vf -eq 'Braces') {@'
\{(?<Variable>\w+)\}| # ... A <Variable> name in {} OR
'@ }
if ($vf -eq 'Brackets') {@'
\[(?<Variable>\w+)\]| # A <Variable> name in [] OR
'@ }
if ($vf -eq 'Tags') {@'
\<(?<Variable>\w+)\>| # A <Variable> name in <> OR
'@ }
if ($vf -eq 'TickDollar') { @'
`\$(?<Variable>\w+) | # A `$ followed by a <Variable> OR
'@ }
if ($vf -eq 'ColonStart') { @'
\:(?<Variable>\w+) # A : followed by a <Variable>
'@ }
}
@"
(?> # A variable can be in a URL segment or subdomain
(?<Start>[/\.]) # Match the <Start>ing slash|dot ...
(?<IsOptional>\?)? # ... an optional ? (to indicate optional) ...
(?:$([Environment]::NewLine + @($varForms -join [Environment]::newline))
)
|
(?<IsOptional> # If it's optional it can also be
[{\[](?<Start>/) # a bracket or brace, followed by a slash
)
(?<Variable>\w+)[}\]] # then a <Variable> name followed by } or ]
$(if (-not $NotInQuery) { @"
| # OR it can be in a query parameter:
(?<Start>[\?\&]) # Match The <Start>ing ? or & ...
(?<Query>[\$\w\-]+) # ... the <Query> parameter name ...
= # ... an equals ...
(?<IsOptional>\?)? # ... an optional ? (to indicate optional) ...
(?:$([Environment]::NewLine + @($varForms -join [Environment]::newline))
)
"@})
)
"@
}