Hello!
On MSDN I found the regular expression that suits me best. But trying to tune it for themselves, faced with the problem that it begins to produce unexpected results.
An example below. It is necessary to snatch from the text fragment {mno ...} there may be an unlimited number of attachments inside.
$content = "{abc}{mno{xyz}}" $mask = "[^{}]*" + "(" + "((?'Open'\{)[^{}]*)+" + "((?'Close-Open'\})[^{}]*)+" + ")*" + "(?(Open)(?!))" $date = [Datetime]::Now $match = [Regex]::Match($content, $mask) if ($match.Success) { $grp = 0 $match.Groups | %{ ("Group {0} : {1}" -f $grp++, $_.Value) $cap = 0 $_.Captures | %{ ("`tCapture {0}: {1}" -f $cap++, $_.Value) } } } ([Datetime]::Now - $date).ToString("hh\:mm\:ss") #Group 0 : {abc}{mno{xyz}} # Capture 0: {abc}{mno{xyz}} #Group 1 : {mno{xyz}} # Capture 0: {abc} # Capture 1: {mno{xyz}} #Group 2 : {xyz # Capture 0: {abc # Capture 1: {mno # Capture 2: {xyz #Group 3 : } # Capture 0: } # Capture 1: } # Capture 2: } #Group 4 : #Group 5 : mno{xyz} # Capture 0: abc # Capture 1: xyz # Capture 2: mno{xyz} I tried to change the mask
$mask = "[^{}]*" + "(" + "((?'Open'\{)mno[^{}]*)+" + "((?'Close-Open'\})[^{}]*)+" + ")*" + "(?(Open)(?!))" but it turns out all garbage
$mask = "[^{}]+{((?>[^{}]+|{(?<c>)|}(?<-c>))*(?(c)(?!)))}"- Wiktor StribiżewGroup 0 : mno{xyz}// Capture 0: mno{xyz}// Group 1 : xyz// Capture 0: xyz// Group 2 :- Wiktor Stribiżew