サンプル文字列の各文字を単純に解析し、ネストされた深さを追跡するルーチンを作成することもできます。
Powershell の例
PowerShell コンソールが手元にあるので、この PowerShell サンプルを提供します。これは、関数がどのように機能するかを示すためだけのものです。
$string = '{a}
{a:b}
a:{b}g{h}ik
{a:{b:c}}
{a}{b}
{a}{b}{c}
{a{b{c}}}{{{d}e}f}
'
$intCount = 0
# split the string on the open and close brackets, the round brackets ensure the squiggly brackets are retained
foreach ($CharacterGroup in $string -split "([{}])") {
write-host $("+" * $intCount)$CharacterGroup
if ($CharacterGroup -match "{") { $intCount += 1 }
if ($CharacterGroup -match "}") { $intCount -= 1 }
if ($intCount -lt 0) {
Write-Host "missing close bracket"
break
} # end if
} # next $CharacterGroup
収量
{
+ a
+ }
{
+ a:b
+ }
a:
{
+ b
+ }
g
{
+ h
+ }
ik
{
+ a:
+ {
++ b:c
++ }
+
+ }
{
+ a
+ }
{
+ b
+ }
{
+ a
+ }
{
+ b
+ }
{
+ c
+ }
{
+ a
+ {
++ b
++ {
+++ c
+++ }
++
++ }
+
+ }
{
+
+ {
++
++ {
+++ d
+++ }
++ e
++ }
+ f
+ }