import Foundation
let pattern = ##"<a(?:\s+name=".*?")?\s+href=.*?#.*?>(.*?)<\/a>"##
let regex = try! NSRegularExpression(pattern: pattern)
let testString = ##"""
<a href="http://example.com/books/1">The Lord of the Rings</a>
<ul>
<li><a href="http://example.com/books/1#c1" >Chapter 1</a></li>
<li><a name="name before href" href="http://example.com/books/1#c2">Chapter 2</a></li>
<li><a href="http://example.com/books/1#c3" name="name after href">Chapter 3</a></li>
<li><a href="http://example.com/books/1#cN" target="_blank">Chapter N</a></li>
</ul>
<br><br>
<a href="http://example.com/books/1">Harry Potter</a>
<ul>
<li><a href="http://example.com/books/2#c1" target="_self">Chapter 1</a></li>
<li><a href="http://example.com/books/2#c2" name="some have name" title="some others have title" >Chapter 2</a></li>
<li><a href="http://example.com/books/2#c3">Chapter 3</a></li>
<li><a href="http://example.com/books/2#cN" >Chapter N</a></li>
</ul>
"""##
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