0

我有一个像这样的正则表达式:

(.*?)("DisplayName":.*?)(,)(.*?"Groups":?)?(\[.*?\])?(,)(.*?"Phones":)?(\[.*?\])?(.*?\},)?

我想用它处理这样的字符串:

{"Affinity":20,"DisplayName":"Moe Larry","Emails":[{"Address":"moelarry@gmail.com","Primary":true,"Type":{"Id":" HOME"}}],"FullName":{"FamilyName":"Larry","GivenName":"Moe","Unstructured":"Moe Larry"},"Groups":[{"id":"^Mine "}],"Id":"1234567890","MailsS​​ent":0,"Name":"Moe Larry","Phones":[{"Number":"555-999-6661","Type":{ "Id":"MOBILE"}}],"ProfileLink":""},{"Affinity":20,"DisplayName":"stoogesarefunny","Emails":[{"Address":"stoogesarefunny","Primary “:真的}],"EvergreenPhoto":"/photos/private/adflk;jsd394u75430o8752380974321jtkasdljf8937489213749832654","Id":"834754hthbf83744823f","MailsS​​ent":0},{"Affinity":20,"DisplayName":"stoogesaref.电子邮件":[{"地址":"stoogesarefunny@gmail.com","Primary":true}],"EvergreenPhoto":"/photos/private/asdfAJDKLJSFIOEJHLTHSJKLDF234987s897KJHSDFKJHDF89273473ASLKJDLSKJIFEIH","Id":"834754hthbf83744823f""Mails:" 0,"个人资料链接":"stoogesarefunny@gmail.com","Emails":[{"地址":"stoogesarefunny@gmail.com","Primary":true}],"EvergreenPhoto":"/photos/private/asdfAJDKLJSFIOEJHLTHSJKLDF234987s897KJHSDFKJHDF89273473ASLKJDLSKJIFEIH","Id": "834754hthbf83744823f","MailsS​​ent":0,"ProfileLink":"stoogesarefunny@gmail.com","Emails":[{"地址":"stoogesarefunny@gmail.com","Primary":true}],"EvergreenPhoto":"/photos/private/asdfAJDKLJSFIOEJHLTHSJKLDF234987s897KJHSDFKJHDF89273473ASLKJDLSKJIFEIH","Id": "834754hthbf83744823f","MailsS​​ent":0,"ProfileLink":"https://profiles.google.com/stoogesarefunny "},{"Affinity":20,"DisplayName":"Shemp","FullName":{"GivenName":"Shemp","Unstructured":"Shemp"} ,"Groups":[{"id":"^Mine"}],"Id":"1234567890","MailsS​​ent":0,"Name":"Shemp","Phones":[{"Number": "+15553085671","Type":{"Id":"OTHER"}}]},{"Affinity":20,"DisplayName":"ClownFace","FullName":{"GivenName":"ClownFace", "非结构化":"ClownFace"},"Groups":[{"id":"^Mine"}],"Id":"1234567890","MailsS​​ent":0,"Name":"ClownFace","电话":[{"Number":"+15556064040","Type":{"Id":"OTHER"}}]},

这真的很难看,我知道。我希望我能找到一个 xml 提要,但现在这不是一个选项。

我只关心 DisplayName、Groups 和 Phones。我需要将它们提取并保存在数组数组中。组和电话的捕获组需要是可选的,因为并非所有联系人都有它们。但是,我的正则表达式给了我:

Result 1

1. {"Affinity":20,
2. "DisplayName":"Moe Larry"
3. ,
4. "Emails":[{"Address":"moelarry@gmail.com","Primary":true,"Type":{"Id":"HOME"}}],"FullName":{"FamilyName":"Larry","GivenName":"Moe","Unstructured":"Moe Larry"},"Groups":
5. [{"id":"^Mine"}]
6. ,
7. "Id":"1234567890","MailsSent":0,"Name":"Moe Larry","Phones":
8. [{"Number":"555-999-6661","Type":{"Id":"MOBILE"}}]
9. ,"ProfileLink":""},

Result 2

1. {"Affinity":20,
2. "DisplayName":"stoogesarefunny"
3. ,
4. "Emails":[{"Address":"stoogesarefunny","Primary":true}],"EvergreenPhoto":"/photos/private/adflk;jsd394u75430o8752380974321jtkasdljf8937489213749832654","Id":"834754hthbf83744823f","MailsSent":0},{"Affinity":20,"DisplayName":"stoogesarefunny@gmail.com","Emails":[{"Address":"stoogesarefunny@gmail.com","Primary":true}],"EvergreenPhoto":"/photos/private/asdfAJDKLJSFIOEJHLTHSJKLDF234987s897KJHSDFKJHDF89273473ASLKJDLSKJIFEIH","Id":"834754hthbf83744823f","MailsSent":0,"ProfileLink":"https://profiles.google.com/stoogesarefunny"},{"Affinity":20,"DisplayName":"Shemp","FullName":{"GivenName":"Shemp","Unstructured":"Shemp"},"Groups":
5. [{"id":"^Mine"}]
6. ,
7. "Id":"1234567890","MailsSent":0,"Name":"Shemp","Phones":
8. [{"Number":"+15553085671","Type":{"Id":"OTHER"}}]
9. },

Result 3

1. {"Affinity":20,
2. "DisplayName":"ClownFace"
3. ,
4. "FullName":{"GivenName":"ClownFace","Unstructured":"ClownFace"},"Groups":
5. [{"id":"^Mine"}]
6. ,
7. "Id":"1234567890","MailsSent":0,"Name":"ClownFace","Phones":
8. [{"Number":"+15556064040","Type":{"Id":"OTHER"}}]
9. },

显然,Shemp 的所有联系数据都被包含在 stoogesarefunny@gmail.com 的数据中,因为我的正则表达式会继续删除,直到到达 Shemps Group,而不是在他的显示名称之前停止并重新开始。帮助?

PS:不,我不打算拯救所有这些团体,最终只是为了研究发生了什么。

4

1 回答 1

6

您的输入看起来像JSON,其中已经有 Ruby 的解析器:

gem install json

然后在红宝石中:

data = JSON.parse(string)

然后您可以data直接作为哈希对象访问,例如:

data = '
  {"Affinity":20,
    "DisplayName":"Moe Larry",
    "Emails":[{"Address":"moelarry@gmail.com","Primary":true,"Type":{"Id":"HOME"}}],
    "FullName":{"FamilyName":"Larry","GivenName":"Moe","Unstructured":"Moe Larry"},
    "Groups":[{"id":"^Mine"}],
    "Id":"1234567890",
    "MailsSent":0,
    "Name":"Moe Larry",
    "Phones":[{"Number":"555-999-6661","Type":{"Id":"MOBILE"}}],
    "ProfileLink":""
  }
'

require 'json'
user = JSON.parse(data)
user.class                    # => Hash
user.keys                     # => ["Affinity", "DisplayName", "Emails", "FullName", "Groups", "Id", "MailsSent", "Name", "Phones", "ProfileLink"]
user['Affinity']              # => 20
user['DisplayName']           # => "Moe Larry"
user['Emails']                # => [{"Address"=>"moelarry@gmail.com", "Primary"=>true, "Type"=>{"Id"=>"HOME"}}]
user['Emails'].class          # => Array
user['Emails'][0]             # => {"Address"=>"moelarry@gmail.com", "Primary"=>true, "Type"=>{"Id"=>"HOME"}}
user['Emails'][0]['Address']  # => "moelarry@gmail.com"
于 2011-12-14T23:44:55.503 回答