2013-03-31 35 views
1

我想下面的字符串解码:Ruby的正则表达式前瞻在管道内来分割

body = '{type:paragaph|class:red|content:[class:intro|body:This is the introduction paragraph.][body:This is the second paragraph.]}' 
body << '{type:image|class:grid|content:[id:1|title:image1][id:2|title:image2][id:3|title:image3]}' 

我需要串在管道拆分而不是其中一个管被包含同方括号,要做到这一点,我想我需要执行一个超前如下所述:How to split string by ',' unless ',' is within brackets using Regex?

我尝试(仍然分裂在每个管):

x = self.body.scan(/\{(.*?)\}/).map {|m| m[0].split(/ *\|(?!\]) */)} 
-> 
[ 
    ["type:paragaph", "class:red", "content:[class:intro", "body:This is the introduction paragraph.][body:This is the second paragraph.]"] 
    ["type:image", "class:grid", "content:[id:1", "title:image1][id:2", "title:image2][id:3", "title:image3]"] 
] 

Expectin G:

-> 
    [ 
     ["type:paragaph", "class:red", "content:[class:intro|body:This is the introduction paragraph.][body:This is the second paragraph.]"] 
     ["type:image", "class:grid", "content:[id:1|title:image1][id:2|title:image2][id:3|title:image3]"] 
    ] 

有谁知道这里所需要的正则表达式?

是否有可能匹配这个正则表达式?我似乎无法正确地修改它Regular Expression to match underscores not surrounded by brackets?


我修改的答案在这里Split string in Ruby, ignoring contents of parentheses?获得:

self.body.scan(/\{(.*?)\}/).map {|m| m[0].split(/\|\s*(?=[^\[\]]*(?:\[|$))/)} 

似乎这样的伎俩。虽然我确定是否有任何不足之处。

+0

您尝试按管道拆分,但不包含一个,只能向前看一个字符,所以没有看到管道。如果你把它放在更前面,你还需要声明没有开放支架。您还需要断言没有以前的开头括号。在这个阶段,值得考虑以不同的方式收集解析结构。 。 。 –

+0

括号是否出现在封装输入字符串位的上下文中?也就是'this | [是一个字符串] |使用孤儿]的值吗? –

+0

不,它们只用于上述情况。 –

回答

1

我修改的答案在这里Split string in Ruby, ignoring contents of parentheses?获得:

self.body.scan(/\{(.*?)\}/).map {|m| m[0].split(/\|\s*(?=[^\[\]]*(?:\[|$))/)} 

似乎这样的伎俩。如果有任何不足之处,请提供更好的建议。

+0

如果在你的结构中没有更深的'{}'或'[]'嵌套,我认为它会好的。你仍然没有完成解析数据,除非你能够按原样使用结果。下一步,我猜想是将每个项目分割在':'(仅限第一个项目)之后,然后检测嵌入式列表并重复您的想法,但使用像'scan(/\[(.*?)\]/) '。 。 。 –

+0

这就是我所拥有的。谢谢你的帮助。 –

3

处理具有相同语法的嵌套结构会给你带来困难。

你可以尝试一个递归下降解析器(快速谷歌打开了https://github.com/Ragmaanir/grammy - 不知道什么好)

就个人而言,我会去真正哈克的东西 - 一些gsubs是将您的字符串转换成JSON,然后使用JSON解析器解析:-)。这并不是特别容易的事,不过,但这里有云:

require 'json' 

b1 = body.gsub(/([^\[\|\]\:\}\{]+)/,'"\1"').gsub(':[',':[{').gsub('][','},{').gsub(']','}]').gsub('}{','},{').gsub('|',',') 


JSON.parse('[' + b1 + ']') 

这并不容易,因为字符串格式显然使用[foo:bar][baz:bam]代表散列的数组。如果您有机会修改序列化格式以使其更容易,我会接受它。

+0

你能推荐更好的sting格式来达到同样的效果吗?字符串的格式很灵活。目标是拥有一个散列数组,内容散列也用于包含另一个散列数组。 –

+0

是否可以使用JSON?它比你的格式略为冗长,但它可以轻松地对需要的结构进行序列化和反序列化,并且解析器已经可以用多种语言提供。 –

+0

我修改了答案在这里http://stackoverflow.com/questions/2015826/split-string-in-ruby-ignoring-contents-of-parentheses得到'self.body.scan(/\{(.*?) \} /)。map {| m | m [0] .split(/ \ | \ s *(?= [^ \ [\]] *(?:\ [| $))/)}'似乎可以做到这一点。 –