import Foundation
let pattern = #"^DTSTART:(\w+)\s*"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:1. Bundesliga
X-WR-TIMEZONE:Europe/Berlin
X-WR-CALDESC:iCal-Spielplan mit allen Spielen der 1. Bundesliga 2013-2014 -
gratis abonnieren!
BEGIN:VEVENT
DTSTART:20150226T200500Z
DTEND:20150226T220500Z
DTSTAMP:20150227T073355Z
UID:jmgapu2jbqhsesbpjlfc495c90@google.com
CREATED:20141216T183608Z
DESCRIPTION:Europa League\, Zw.\n\nhttp://www.fussball-spielplan.de
LAST-MODIFIED:20150226T221219Z
LOCATION:
SEQUENCE:3
STATUS:CONFIRMED
SUMMARY:Sporting Lissabon - VfL Wolfsburg (0:0)
TRANSP:TRANSPARENT
END:VEVENT
BEGIN:VEVENT
DTSTART:20150226T180000Z
DTEND:20150226T200000Z
DTSTAMP:20150227T073355Z
UID:uorcsvdsc24f37frgm36f3sfak@google.com
CREATED:20141216T164808Z
DESCRIPTION:Europa League\, Zw.\n\nhttp://www.fussball-spielplan.de
LAST-MODIFIED:20150226T200016Z
LOCATION:Borussia-Park\, Mönchengladbach
SEQUENCE:3
STATUS:CONFIRMED
SUMMARY:Bor. Mönchengladbach - FC Sevilla (2:3)
TRANSP:TRANSPARENT
END:VEVENT
BEGIN:VEVENT
DTSTART:20150225T194500Z
"""#
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