import Foundation
let pattern = #"^(\s*)object\s(\w*):\sTcxRadioGroup.*?\n\1end"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .dotMatchesLineSeparators])
let testString = #"""
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 494
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object cxRadioGroup1: TcxRadioGroup
Left = 0
Top = 0
Align = alTop
Caption = 'cxRadioGroup1'
Properties.Items = <>
TabOrder = 0
ExplicitLeft = 24
ExplicitTop = 16
ExplicitWidth = 185
Height = 105
Width = 635
end
object cxRadioGroup2: TcxRadioGroup
Left = 0
Top = 389
Align = alBottom
Properties.Items = <>
TabOrder = 1
ExplicitTop = 293
Height = 105
Width = 635
end
object Panel1: TPanel
Left = 200
Top = 111
Width = 257
Height = 265
Caption = 'Panel1'
TabOrder = 2
object cxRadioGroup3: TcxRadioGroup
Left = 8
Top = 16
Caption = 'cxRadioGroup3'
Properties.Items = <>
TabOrder = 0
Height = 105
Width = 185
end
object cxRadioGroup4: TcxRadioGroup
Left = 8
Top = 144
Properties.Items = <
item
Caption = 'item 1'
end
item
Caption = 'item 2'
end>
TabOrder = 1
Height = 105
Width = 185
end
end
end
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression