-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathScriptBlock.regex.txt
37 lines (37 loc) · 1.02 KB
/
ScriptBlock.regex.txt
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
# Matches a PowerShell script block
\{
\s{0,}
(?:
\<\# # The opening tag
(?<Help>
(?:.|\s)+?(?=\#>) # anything until the closing tag
)
\#\> # the closing tag
)?
\s{0,}
(?:
\s{0,}
(?<Attributes>?<PowerShell_Attribute>([))
\s{0,}
){0,}
\s{0,}
(?<ParamBlock>
param?<BalancedCode>{(}
)?
(?:
\s{0,}
(?<NamedBlock>(?:begin|dynamicParam|process|end)\s{0,}
(?<NamedScriptBlock>?<BalancedCode>({)))
\s{0,}
)?
(?(NamedBlock) # If we have a begin, process, or end
(?:\}) # then match but don't capture the closing }
| # otherwise, assume we're in an end block
(?<EndBlock> # 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))
)