我正在尝试在 MacOS 上快速解析一个小型项目的 Localizable.string 文件。我只想检索文件中的所有键和值以将它们分类到字典中。
为此,我在NSRegularExpression可可类中使用了正则表达式。
这是这些文件的样子:
"key 1" = "Value 1";
"key 2" = "Value 2";
"key 3" = "Value 3";
Run Code Online (Sandbox Code Playgroud)
这是我的代码,它应该从加载到 a 的文件中获取键和值String:
static func getDictionaryFormText(text: String) -> [String: String] {
var dict: [String : String] = [:]
let exp = "\"(.*)\"[ ]*=[ ]*\"(.*)\";"
for line in text.components(separatedBy: "\n") {
let match = self.matches(for: exp, in: line)
// Following line can be uncommented when working
//dict[match[0]] = match[1]
print("(\(match.count)) matches = \(match)")
}
return …Run Code Online (Sandbox Code Playgroud)