import Foundation
let pattern = #"^[^\S\r\n]+artist:[^\S\r\n]*(.*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
mp3 @ 0x75b225b8]Estimating duration from bitrate, this may be inaccurate
[lavf] stream 0: audio (mp3), -aid 0
[lavf] stream 1: video (mjpeg), -vid 0,
Clip info:
Release date: Songspk.LINK
album_artist: Various Artists
album: Kapoor & Sons (Since 1921)
artist: Tanishk Bagchi, Arijit Singh & Asees Kaur
composer: Amaal Mallik | Songspk.LINK
disc: 1/1
encoded_by: Lame
genre: Bollywood Music
title: Bolna - Songspk.LINK
track: 1/1
EpisodeID: Songspk.LINK
copyright: Songspk.LINK
TOPE: Amaal Mallik | Songspk.LINK
TIT1: Songspk.LINK
TIT3: Songspk.LINK
"""#
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