import Foundation
let pattern = #"."#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tincidunt pellentesque ex tempor porta. Ut <a href="http://molestielectus.com">molestie lectus</a> egestas lectus ornare, eu pellentesque ante posuere.
Nunc volutpat non tellus tincidunt tincidunt. Aenean scelerisque sit amet felis eget vestibulum. <a title="Fusce blandit" href="https://www.fusceblandit.com">Fusce blandit</a> bibendum ligula, et viverra nunc faucibus in. Etiam hendrerit in turpis eget varius.
Phasellus pellentesque porttitor laoreet. Curabitur pharetra cursus sapien, non gravida libero <a href="mailto:feugiat@fermentum.com">feugiat fermentum</a>. Nam bibendum auctor condimentum. Integer ut rhoncus odio, facilisis aliquam orci.
Ut placerat nisi urna, eget ultricies enim gravida ac. Praesent sed aliquam urna. Etiam ornare purus a sapien <a href='//vulputatemattis.com'>vulputate mattis</a>.
Morbi tempor imperdiet risus, quis tristique nunc varius et. Praesent elementum at arcu nec malesuada. <a href="//integerporta.com" title="Integer porta">Integer porta</a> lorem non commodo eleifend. Curabitur ut risus non libero volutpat posuere.
"""#
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