import Foundation
let pattern = #"^(?:https?:\/\/)?(?:www\.)?[\d\w\.-]+(?:\.|:)(?:[a-z\.]{2,6}|\d+)\/?.*$"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = #"""
https://regex101.com/
https://www.variables.sh/complex-regular-expression-examples
https://search.brave.com/search?q=regex+challenges&source=desktop
https://web.whatsapp.com/
https://www.youtube.com/watch?v=fc_RvxTtIwc
https://text-auditor.netlify.app/
https://github.com/Dashrath-Sharma
http://github.com/Dashrath-Sharma
https://onecompiler.com/javascript/3zqzyu2g4
http://localhost:3000/
https://www.codingame.com/clashofcode/clash/report/3342454a92e4c660d772438bac8b2ef73de3c8f
https://react.dev/learn/thinking-in-react
https://dapp-world.com/course/introduction-to-solidity-Ynws/working-of-traditional-dapps-compared-to-dapps/reading
http://www.google.com
https://www.google.com
http://google.com
https://google.com
http://www.google.com/
https://www.google.com/
http://google.com/
https://google.com/
http://www.google.com/index.html
https://www.google.com/index.html
http://google.com/index.html
https://google.com/index.html
"""#
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