import Foundation
let pattern = ##"(?mis)^\bgeneral( #[0-9])?(.*?)^\s*$"##
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
General
Complete name : C:\Projects\Foray\MediaEngine\SampleMedia\flv\small.flv
Format : Flash Video
File size : 296 KiB
Duration : 5s 560ms
Overall bit rate mode : Variable
Overall bit rate : 436 Kbps
Writing application : Lavf52.103.0
Video
Format : Sorenson Spark
Codec ID : 2
Duration : 5s 560ms
Bit rate : 781 Kbps
Width : 320 pixels
Height : 240 pixels
Display aspect ratio : 4:3
Frame rate mode : Constant
Frame rate : 25.000 fps
Bit depth : 8 bits
Bits/(Pixel*Frame) : 0.407
Stream size : 530 KiB
Audio
Format : MPEG Audio
Format version : Version 1
Format profile : Layer 3
Codec ID : 2
Codec ID/Hint : MP3
Duration : 5s 512ms
Bit rate mode : Variable
Bit rate : 63.8 Kbps
Channel(s) : 1 channel
Sampling rate : 44.1 KHz
Compression mode : Lossy
Stream size : 42.9 KiB (14%)
"""#
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