import Foundation
let pattern = ##"^(?:#JSGF) (?<version>V1.0) (?<encoding>[A-Z-0-9]+) (?<locale>[a-z]{2}-[A-Z]{2});$"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
#JSGF V1.0 UTF-8 hu-HU;
grammar planmaster.hu;
<command_change_odontogram> = ( <change_to> [ a ] <odontogram_mode> [ felvételére | felvételre ] ) | ( <odontogram_mode> felvétele ) ;
<change_to> = ( váltás | válts | menj | ugrás | ugorj ) ;
<odontogram_mode> = ( státuszra | státuszhoz | státusz ) {state} | ( kezelés | kezelésre | kezeléshez | lépésekre | lépésekhez ) {treatment} ;
<command_full_jaw> = <all> * ( alsó {lower} | felső {upper} ) [ fogsor | állkapocs | fogak | fog ] <all> * <status> ;
<all> = mind | összes | teljes ;
<status> = ( <status1> | <status2> | <status3> ) {status} ;
<status1> = ( ép | alaphelyzet | alaphelyzetbe ) {1} ;
<status2> = ( hiány | hiányzik ) {2} ;
<status3> = [ szekunder ] ( szuvas | szuvasodott | szuvasodás ) {3} ;
<tooth18> = ( 18as | 18-as | tizennyolcas | nyolcas | ( 18 as ) ) {18} ;
<tooth17> = ( 17es | 17-es | tizenhetes | hetes | ( 17 es ) ) {17} ;
<tooth16> = ( 16os | 16-os | tizenhatos | hatos ) {16} ;
<tooth_region> = ( bal {left} | jobb {right} ) ( felső {upper} | alsó {lower} ) ;
<tooth_number> = ( <tooth18> | <tooth17> | <tooth16> | és ) ;
<command_select> = ( ( <tooth_number> + ) | <tooth_region> <tooth_number> + ) [ fog | fogak ] ;
"""##
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