import Foundation
let pattern = #"\b([^\s\"]+)(\s+|)\:=\s+new\s+([^(]+)\(([^;\(\)]+)\);"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = #"""
create or replace package body ORA_MTK.Test_package_03 as
procedure do_something (n1 in NUMBER, n2 in out NUMBER,n3 in out NOCOPY NUMBER) as
aaa11 ty_base := new ty_base();
l_array12 tyt_base := new tyt_base(ty_base(1,2,3));
aaa13 ty_base:= new ty_base(participantid => 'a',clientid => '1',price => 3);
begin
aaa11 ty_base := new ty_base();
l_array12 tyt_base := new tyt_base(ty_base(1,2,3));
l_array13 := new tyt_base(ty_base(1,2,3));
aaa13 := new ty_base(participantid => 'a',clientid => '1',price => 3);
aaa14 := new ty_base(participantid => 'a', -- comment 01
clientid => '1' -- comment 01 ; ,
,price => 3);
aaa15 := new ty_base(participantid15 => 'a', -- comment 01
-- comment 02;
clientid15 => '1'
,price15 => 3);
aaa16.xxxx := new ty_base(participantid => 'a',clientid => '1',price => 3);
l_array(l_array.count) := NEW ty_base(participantid => 300,clientid => 2,price => 3);
l_array(l_array.count).xxxx := NEW ty_base(participantid => 300,clientid => 2,price => 3);
"aaa 17" := new ty_base(participantid => 'a',clientid => '1',price => 3);
end do_something;
end Test_package_03
"""#
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