我有一个示例podfile.lock,如下所示:
PODS:
- MyLibrary1 (0.10.0):
- MyLibrary1A (>= 0.9.0)
- AFNetworking (1.1.0)
- MyLibrary2 (3.0.0)
- Objective-C-HMTL-Parser (0.0.1)
DEPENDENCIES:
- MyLibrary1 (= 0.10.0)
- AFNetworking (= 1.1.0)
- MyLibrary2 (= 3.0.0)
- Objective-C-HMTL-Parser (= 0.0.1)
SPEC CHECKSUMS:
MyLibrary1: 1d65fa5d905211c8981a11091eae8c98b410eb19
AFNetworking: 761a418233d280c1e0580bcb76ff9cb4c3073d17
MyLibrary2: 35b80d5cc522b0f53beb5583c635e42ecce69743
Objective-C-HMTL-Parser: 4fca85e186a829cfe8db8e316e9688571ea4dfed
COCOAPODS: 0.16.9
需要量:
我希望依赖关系数据之间的数据采用JSON格式,例如:
{
"MyLibrary1": "0.10.0",
"AFNetworking": "1.1.0",
"MyLibrary2": "3.0.0",
"Objective-C-HMTL-Parser": "0.0.1"
}
我做了什么:
我试图首先使用正则表达式获取“依赖项:”中的数据:
start_string = "DEPENDENCIES:"
end_string = "SPEC CHECKSUMS:"
file_contents[/#{start_string}(.*?)#{end_string}/m, 1]
有了这个我得到:
"\n - MyLibrary1 (= 0.10.0)\n - AFNetworking (= 1.1.0)\n - MyLibrary2 (= 3.0.0)\n - Objective-C-HMTL-Parser (= 0.0.1)\n\n"
问题:
使用上面发布的正则表达式的结果,如何/获取/分离“MyLibrary1”和“0.10.0”来制作JSON的简单方法是什么?
答案 0 :(得分:3)
您可以使用标记最后一场比赛结束位置的\G
锚点:
data.scan /(?:\G(?!\A)|^DEPENDENCIES:\n)- (\S+) \(= ([^)]+)\)\n/
=> [["MyLibrary1", "0.10.0"], ["AFNetworking", "1.1.0"], ["MyLibrary2", "3.0.0"], ["Objective-C-HMTL-Parser", "0.0.1"]]