import Foundation
let pattern = ##"(?<![src|href]\=\"(\n*|\s*)?)((?:https?):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])"##
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = ##"""
http://www.naver.com
http://www.google.com
http://www.google.com
http://www.naver.com
https://www.naver.com
www.naver.com
http://naver.com
www.naver.com
<img style="vertical-align: top;" alt="" src=" https://www.kita.net/mailclub/DailyTradeNews/images/gk_top_banner_191111.jpg" border="0">
<a target="_blank" href="https://www.kita.net/asocGuidance/notice/noticeDetail.do?pageIndex=1&nIndex=1792315&indexArray=&sortArray=&searchReqType=detail&searchStartDate=&searchEndDate=&searchCommCode1=&searchCondition=TITLE&searchKeyword=">
<a target="_blank" href=" https://www.kita.net/asocGuidance/notice/noticeDetail.do?pageIndex=1&nIndex=1792315&indexArray=&sortArray=&searchReqType=detail&searchStartDate=&searchEndDate=&searchCommCode1=&searchCondition=TITLE&searchKeyword=">
<a target="_blank" href="
https://www.kita.net/asocGuidance/notice/noticeDetail.do?pageIndex=1&nIndex=1792315&indexArray=&sortArray=&searchReqType=detail&searchStartDate=&searchEndDate=&searchCommCode1=&searchCondition=TITLE&searchKeyword=">
<a target="_blank" href= "
https://www.kita.net/asocGuidance/notice/noticeDetail.do?pageIndex=1&nIndex=1792315&indexArray=&sortArray=&searchReqType=detail&searchStartDate=&searchEndDate=&searchCommCode1=&searchCondition=TITLE&searchKeyword=">
(?<!\=\"(\n|\s).?)((?:https?):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])
(^|[\s\n]|<[A-Za-z]*\/?>)((?:https?|ftp):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])
//최종
(?<![src|href]\=\"(\n*|\s*)?)((?:https?):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])
"""##
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