-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBalancedCode.regex.ps1
More file actions
30 lines (28 loc) · 881 Bytes
/
BalancedCode.regex.ps1
File metadata and controls
30 lines (28 loc) · 881 Bytes
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
<#
.Synopsis
Matches Balanced Code
.Description
Matches code balanced by a [, {, or (
#>
param(
[ValidateSet('[','{', '(')]
$Open = '{'
)
$comment =
if ($open -eq '[') { 'bracket' }
elseif ($open -eq '{') { 'brace' }
elseif ($Open -eq '(') { 'parenthesis' }
$close =
if ($Open -eq '[') { ']' }
elseif ($Open -eq '{') { '}' }
elseif ($Open -eq '(') { ')' }
# Matches content in brackets, as long as it is balanced
@"
\$open # An open $comment
(?> # Followed by...
[^\$open\$close]+| # any number of non-$comment character OR
\$open(?<Depth>)| # an open $comment (in which case increment depth) OR
\$close(?<-Depth>) # a closed $comment (in which case decrement depth)
)*(?(Depth)(?!)) # until depth is 0.
\$close # followed by a closing $comment
"@