import Foundation
// WARNING: You included a flag that Swift doesn't support: U
// When this flag is set, it inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by '?'.
// As an alternative, this effect can also be achieved by setting a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).
let pattern = #"(?:\[url=(?:"|"|\'|)(?:(?:(?:(?:(?!https?|ftp|mailto).)*):(?://)?)(?:(?!"|"|"e;).)++)(?:"|"|\'|)\])(?:(?!\[/url\]).)++(?:\[/url\])"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive, .dotMatchesLineSeparators])
let testString = #"[url="snafu://xy.sf.net/cgi-bin/xidelcgi?&data=%3Ctable%20class%3D%22ClassGrid%22%20cellspacing%3D%220%22%20cellpadding%3D%220%22%20border%3D%220%22%20id%3D%22_ctl0_phMainContent_dgrdClasses%22%20style%3D%22border-collapse%3Acollapse%3B%22%3E%0A%3Ctbody%3E%0A%20%20%20%20%3Ctr%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow1%22%20colspan%3D%223%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Chr%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%3C%2Ftr%3E%0A%20%20%20%20%3Ctr%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow1%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox1%22%3EAddress%20123%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cbr%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cbr%3E%3Ca%20target%3D%22_blank%22%20class%3D%22gridDirections%22%20href%3D%22%2FClasses%2FDirections.aspx%23104%22%3EDirections%3C%2Fa%3E%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow2%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox2%22%3E12%2F12%2F2018%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow3%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox3%22%3E%3Ca%20id%3D%22_ctl0_phMainContent_dgrdClasses__ctl3_hplAddToCart%22%20class%3D%22whitelight%22%20href%3D%22%2Fvalidate.aspx%3FClassID1%3D4233%26amp%3BClassID2%3D0%22%3EBook%20Now%3C%2Fa%3E%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%3C%2Ftr%3E%0A%20%20%20%20%3Ctr%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow1%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox1%22%3E%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow2%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox2%22%3E1%2F24%2F2019%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow3%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox3%22%3E%3Ca%20id%3D%22_ctl0_phMainContent_dgrdClasses__ctl4_hplAddToCart%22%20class%3D%22whitelight%22%20href%3D%22%2Fvalidate.aspx%3FClassID1%3D4306%26amp%3BClassID2%3D0%22%3EBook%20Now%3C%2Fa%3E%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%3C%2Ftr%3E%0A%20%20%20%20%3Ctr%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow1%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox1%22%3E%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow2%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox2%22%3ESaturday%2C%20August%204%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow3%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBoxNone%22%3E%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%3C%2Ftr%3E%0A%20%20%20%20%3Ctr%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow1%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox1%22%3E%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow2%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox2%22%3ESaturday%2C%20August%2018%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%20%20%20%20%3Ctd%20class%3D%22ClassGridRow3%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20class%3D%22ClassGridBox3%22%3E%3Ca%20id%3D%22_ctl0_phMainContent_dgrdClasses__ctl6_hplAddToCart%22%20class%3D%22whitelight%22%20href%3D%22%2Fvalidate.aspx%3FClassID1%3D4346%26amp%3BClassID2%3D0%22%3EBook%20Now%3C%2Fa%3E%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Ftd%3E%0A%20%20%20%20%3C%2Ftr%3E%0A%3C%2Ftbody%3E&=&extract=%2F%2Ftbody%2Ftr%2Ftd%2Fdiv%5B%40class%3D%22ClassGridBox1%22%5D%2F%5Breplace(text()%2C'(%5E%5Ba-zA-Z.-%5D%2B%20%5B0-9%5D%2B).*'%2C'%241'%2C%20's')%5D&=%2F%2Ftbody%2Ftr%2Ftd%2Fdiv%5B%40class%3D%22ClassGridBox1%22%5D%2F%5Breplace(text()%2C'(%5E%5Ba-zA-Z.-%5D%2B%20%5B0-9%5D%2B).*'%2C'%241'%2C%20's')%5D&input-format=auto&printed-node-format=xml&output-format=adhoc&compatibility=Enable%20all%20extensions&dot-notation=unambiguous&extract-kind=xpath2&print-type-annotations=true"]example[/url]"#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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