如何使用Ruby的'scan'方法创建捕获组可选?

时间:2011-12-14 23:39:59

标签: ruby regex regex-group capturing-group

我有这样的正则表达式:

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

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

  

{“Affinity”:20,“DisplayName”:“Moe Larry”,“电子邮件”:[{“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 “:” 移动设备 “}}],” ProfileLink “:” “},{” 亲和力 “:20,” 显示名称 “:” stoogesarefunny”, “电子邮件”:[{ “地址”: “stoogesarefunny” , “主”:真}], “EvergreenPhoto”: “/照片/私人/ adflk; jsd394u75430o8752380974321jtkasdljf8937489213749832654”, “ID”: “834754hthbf83744823f”, “MailsS​​ent”:0},{ “亲和力”:20, “显示名称”: “stoogesarefunny@gmail.com”, “电子邮件”:[{ “地址”: “stoogesarefunny@gmail.com”, “主”:真正}], “EvergreenPhoto”: “/照片/私营/ asdfAJDKLJSFIOEJHLTHSJKLDF234987s897KJHSDFKJHDF89273473ASLKJDLSKJIFEIH”, “ID” : “834754hthbf83744823f”, “MailsS​​ent”:0 “ProfileLink”: “https://profiles.google.com/stoogesarefunny”},{ “亲和力”:20, “显示名称”: “Shemp”, “全名”:{ “给定名称”:“Shemp “ ”非结构化“: ”Shemp“}, ”组“:[{” 我d “:” ^矿 “}],” ID “:” 1234567890" , “MailsS​​ent”:0, “名称”: “Shemp”, “电话”:[{ “号码”: “+ 15553085671”, “类型” :{ “ID”: “OTHER”}}]},{ “亲和力”:20, “显示名称”: “ClownFace”, “全名”:{ “给定名称”: “ClownFace”, “非结构化”: “ClownFace”} , “组”:[{ “ID”: “^矿”}], “ID”: “1234567890”, “MailsS​​ent”:0, “名”: “ClownFace”, “手机”:[{ “数”: “15556064040”, “类型”:{ “ID”: “OTHER”}}]},

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

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

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而不是在他的显示名称之前停止并重新开始。帮助

P.S。:不,我不打算保存所有这些组,最终,我只能这样研究发生了什么。

1 个答案:

答案 0 :(得分: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"