I wrote some regular expression code attempting to match nested begin-end code block, like in the pascal language. Example:
begin
begin
stuff1
end
end
begin
stuff2
end
when i run by code below i want to match:
begin
begin
stuff1
end
end
with out the last "begin stuff2 end" pair...
Here's my attempt using windows powershell as the scripting language:
### POWERSHELL
$ErrorActionPreference = 'Stop';
# Want to match balanced begin-end block include nested begin-end:
# begin begin stuff1 end end
$t1 = ""
$t1 += " begin"
$t1 += " begin"
$ti += " stuff1"
$t1 += " end"
$t1 += " end"
$t1 += ""
$t1 += " begin"
$ti += " stuff2"
$t1 += " end"
write-host "LINE: $t1"
$rxop = [Text.RegularExpressions.RegexOptions]::IgnorePatternWhitespace -bor
[Text.RegularExpressions.RegexOptions]::IgnoreCase
$rx = [Regex]::new(
"^(
\bbegin\b
(?:
(?<openp> \bbegin\b )
|
(?<-openp> \bend\b )
|
[^\t]+
)+
(?(openp)(?!))
\bend\b)
",
$rxop
)
$match = $rx.Match($t1)
if ($match.Success) {
$name = $match.Groups[1].Value
write-host "matched: $name"
}
else {
write-host "no-match"
}
basically it doesn't work.