import Foundation
let pattern = #"<a.*(\[nggallery[^\]+]*\]).*?<\/a>"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
<a href="[nggallery id=xx]"><span class="shutterset"><img class="alignnone size-large wp-image-23067" title="Image Title" src="http://example.com/wp-content/uploads/2015/06/image-title.jpg" alt="" width="685" height="456" /></span></a>
<a href="[nggallery id=xxxxx]">Click here!</a>
<a title="title title" href="[nggallery id=xxx]" target="_blank">Title Link Title Link</a>
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"$1"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
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