import Foundation
let pattern = #"^https?:\/\/(m\.)?vk\.com\/((club|public)(\d+)|[_.a-zA-Z0-9]{5,32})?(\?w=)?wall(-\d+)_(\d+)\??.*?&?.*?$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
https://vk.com/club
https://vk.com/id
https://vk.com/id123
https://vk.com/club213560530
https://m.vk.com/club213560530
https://m.vk.com/club213560530
https://vk.com/wall-212141395_
https://vk.com/wall1_1
https://vk.com/ps4exploit?w=wall-212141395_852
https://vk.com/wall-212141395_852
https://vk.com/wall-110273246_191287?access_key=4d0bb719e17dc1433e
https://vk.com/wall-110273246_191287?access_key=
https://vk.com/wall-110273246_191287?foo
https://vk.com/wall-110273246_191287?
https://vk.com/wall-110273246_?access_key=4d0bb719e17dc1433e
https://vk.com/wall-110273246_191287?access_key=4d0bb719e17dc1433e&hello=world
https://vk.com/ps4exploit?w=wall-212141395_852&foo
"""#
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