import Foundation
let pattern = #"^(?<atributo>(?<pai>(?:[^=.\n]+)\.?){1,6})=(?<valor>(?:[^=\n]+))$"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .dotMatchesLineSeparators])
let testString = #"""
php.environment.ini.session-name = sisicmbio
php.environment.ini.session-cookie_domain = icmbio.gov.br
php.environment.ini.session-auto_start = false
php.environment.ini.session-cookie_path = /
php.environment.ini.session-cookie_lifetime = 10800
php.environment.ini.session-cookie_httponly = true
php.environment.ini.session-use_only_cookie = true
php.environment.ini.session-gc_maxlifetime = 10800
php.environment.ini.session-cache_expire = 180
;; [php server mail]
php.mail.sender = smtp
php.mail.from = notifica@domain.gov.br ; conta default usada para enviar mensagens
php.mail.replyTo = reposta@domain.gov.br ; conta usada para reply
php.mail.wordWrap = 60
php.mail.priority = 3 ; 1: high, 3: normal, 5: low
php.mail.encoding = 8bit ; 8bit, 7bit, binary, base64, quoted-printable
php.mail.charset = utf-8
php.mail.contentType = text/html
"""#
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