import Foundation
let pattern = ##"\[img.*\].*photobucket.*(.jpg)\[\/img.*\]"##
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = ##"""
Connecting rod fitted to prevent steering tie rod from cranking:
[url=http://s557.photobucket.com/user/realboss7669/media/F707F408-829F-48B9-9AED-BB57CEFF96C0_zpslwioah0z.jpg.html:jaeyh8wg][img:jaeyh8wg]http://i557.photobucket.com/albums/ss16/realboss7669/F707F408-829F-48B9-9AED-BB57CEFF96C0_zpslwioah0z.jpg[/img:jaeyh8wg][/url:jaeyh8wg]
From the rear:
[url=http://s557.photobucket.com/user/realboss7669/media/05AE0DB2-83FD-4B2C-9FDA-E3257DFEB832_zpsc6cfuqwi.jpg.html:jaeyh8wg][img:jaeyh8wg]http://i557.photobucket.com/albums/ss16/realboss7669/05AE0DB2-83FD-4B2C-9FDA-E3257DFEB832_zpsc6cfuqwi.jpg~original[/img:jaeyh8wg][/url:jaeyh8wg]
Steering damper mounts finished - without the connecting rod the damper would get bent:
[url=http://s557.photobucket.com/user/realboss7669/media/C85A0F5D-413D-4779-9F09-BD43BB798238_zpse8s6dxyu.jpg.html:jaeyh8wg][img:jaeyh8wg]http://i557.photobucket.com/albums/ss16/realboss7669/C85A0F5D-413D-4779-9F09-BD43BB798238_zpse8s6dxyu.jpg[/img:jaeyh8wg][/url:jaeyh8wg]
<!-- s8) --><img src="{SMILIES_PATH}/icon_cool.gif" alt="8)" title="Cool" /><!-- s8) -->
"""##
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