我想下面的字符串解码: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*(?=[^\[\]]*(?:\[|$))/)}
似乎这样的伎俩。虽然我确定是否有任何不足之处。
您尝试按管道拆分,但不包含一个,只能向前看一个字符,所以没有看到管道。如果你把它放在更前面,你还需要声明没有开放支架。您还需要断言没有以前的开头括号。在这个阶段,值得考虑以不同的方式收集解析结构。 。 。 –
括号是否出现在封装输入字符串位的上下文中?也就是'this | [是一个字符串] |使用孤儿]的值吗? –
不,它们只用于上述情况。 –