小编Edg*_*gar的帖子

试图在 MacOS 上快速解析一个小项目的 Localizable.string 文件

我正在尝试在 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)

regex string macos nsregularexpression swift

4
推荐指数
1
解决办法
2273
查看次数

标签 统计

macos ×1

nsregularexpression ×1

regex ×1

string ×1

swift ×1