import Foundation
let pattern = ##"""
^(https?:\/\/)? #протокол
([\w-]{1,32}\.[\w-]{1,32}) #домен
[^\s@]* #любой не пробельный символ + @
$
"""##
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .allowCommentsAndWhitespace])
let testString = #"""
/********************** yes *****************/
http://yan-dex.ru
http://asd-asd.ru
sdf-sdf.ru
http://sdfsdf.ru/sadasds-sadsad/sadsad
http://sdfsdf.ru/sadasds-sadsad/sadsad?page=2
http://sdfsdf.ru/sadasds-sadsad/sadsad?page=2&other=1
sdfsdf.ru/sadasds-sadsad/sadsad?page=2&other=1
http://sdfsdf.ru/sadasds/sadsad?page=2&other=1
https://regex101.com/r/tM7Pmr/1
http://regexpres.narod.ru/calculator.html
http://regexpres.narod.ru/calculator.htm
http://regexpres.narod.ru/calculator.php
http://regexpres.narod.ru/index.php
http://regexpres.narod.ru/index
https://jsfiddle.net/3b0khwhasd6/
https://lenta.ru/articles/2017/03/06/ruvlogs/
https://www.youtube.com/watch?v=PDxVcTWVIck
www.youtube.com/watch?v=PDxVcTWVIck
https://zakon.ru/blog/2017/03/06/delo_o_denezhnyh_generatorah_i_udobnom_sude__p-v_protiv_ikea_opredelenie_skgd_vs_ot_17012017_36-kg16
https://zakon.ru/konstitucionnyj_sud_rf
zakon.ru/blog/2017/03/06/delo_o_denezhnyh_generatorah_i_udobnom_sude__p-v_protiv_ikea_opredelenie_skgd_vs_ot_17012017_36-kg16
https://zakon.ru/konstitucionnyj_sud_rf
/********************** not *****************/
4234czxc
sdfsdf
sdsad.rturturturt/fsdfsd@sd
"""#
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