import Foundation
let pattern = #"^(?:.*[\/\\])?(?<FileNameWithoutExtension>.*)\.(.*)$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
Taylor Swift - New Romantics.m4a
YT/Subto2mp3/Taylor Swift - New Terribly Romantics.m4a
Grow - Conan Blue (Official Video) has already been recorded in archive.m4a
PowerOptim Iol/Techno/(Official Video) Blue Burrow was already been recorded in archive - Bak.wm4
/Great Bun/Output Files/çè/Grow - Conan DARK (Official AUDIO) has already been recorded in archive.m4b
Matter Oil_9999.zip
Qpl$-pol.hrlp/perm.ui.pol/i op/doesn't matter.mp4
Small Laser4.m4a
manicur pliu/love is in the a°°_II+?§@R.txt
^Level09\\Power Pol\energy.m4a
lollller_blllade 1987 (c)/super €/music_piece_.mp3
\.\.$./../../loli( u1990) _98.lop.pl/fil l/fill2asm.aile.asm.gz.zip
/Taylor Swift - New Romantics._m4a
\$/Subto2/mp3/Taylor Swift -&- New Romantics.m4a
/Grow - Conan Gray (Official Video) has already been recorded in archive.m4a
C:\PowerOptim Iol/Grow - The New Gray (Official Video) has been recorded [<1999>.wm4
/Great Bun/Output Files/çè/Grow - WiLlL(O)W Gray "unofficial" video has not been recorded in archive.m4b
/Matter Oil_9999.zip
file:///Qpl$-pol.hrlp/perm.ui.pol/i op/matter.mp4
/£¥«^6<4>à_»Laser4 ``[12{2%}32]deg`‘.webm
manicur pliu/great.power.great.responsibility?.gz.tar.zip
./poi kiol/greater.power.greater.responsibility!.gz.tar.zip
^Level09\Power Pol\energy.m4a
lollller_blllade 1987 (c)/super €/music_piece_.mp3
/The 'great' solitude of zorro #12/the 退ellent urge_for.mo.tar.gz
"""##
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