import Foundation
let pattern = #"([\:][\/]{0,2})[\w\-]+([\@]|[\.])?([\w\-]+[\.]){0,}([\w\-]+)?([\:][\d]+)?[\/]?"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = ##"""
https://user_test-teste@regex101.com:80/
http://localhost:80/
https://tools.ietf.org/html/rfc3986#page-7
https://translate.google.com.br/#en/pt/that%20what
https://www.google.com.br/search?client=firefox-b&biw=1920&bih=1001&q=overload+inexistent+property+php&oq=overload+inexistent+property+php&gs_l=psy-ab.3..33i22i29i30k1.36919.57166.0.57324.38.36.2.0.0.0.682.3958.14j18j5-1.33.0....0...1.1.64.psy-ab..3.27.3324...0j35i39k1j0i131k1j0i67k1j0i10k1j0i22i30k1j33i160k1j33i21k1.m5dBRh4nQ-I
"""##
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