问题描述
我正在实现某种解析器,我需要定位和反序列化嵌入到其他半结构化数据中的json对象 。我使用了regexp:
I'm implementing some kind of parser and I need to locate and deserialize json object embedded into other semi-structured data. I used regexp:
\\{\\s*title.*?\\}
找到对象
{title:'Title'}
但它不适用于嵌套对象,因为表达式只匹配第一个找到的结束花括号。对于
but it doesn't work with nested objects because expression matches only first found closing curly bracket. For
{title:'Title',{data:'Data'}}
匹配
{title:'Title',{data:'Data'}
因此字符串对于反序列化无效。
我知道有一个贪婪的业务,但我不熟悉正则表达式。你能否帮我扩展表达式以消耗所有可用的结束花括号。
so string becomes invalid for deserialization.I understand that there's a greedy business coming into account but I'm not familiar with regexps. Could you please help me to extend expression to consume all available closing curly brackets.
更新:
为了清楚起见,这是尝试从带有嵌入式JSON的HTML + JS等半结构化数据中提取JSON数据。我正在使用GSon JAVA lib来实际解析提取的JSON。
To be clear, this is an attempt to extract JSON data from semi-structured data like HTML+JS with embedded JSON. I'm using GSon JAVA lib to actually parse extracted JSON.
推荐答案
感谢@Sanjay T. Sharma指出我支持匹配因为我最终对贪婪的表达有了一些了解,也感谢其他人最初说我不该做的事情。
幸运的是,结果表明可以使用贪婪的表达变体
Thanks to @Sanjay T. Sharma that pointed me to "brace matching" because I eventually got some understanding of greedy expressions and also thanks to others for saying initially what I shouldn't do.Fortunately it turned out it's OK to use greedy variant of expression
\\{\s*title.*\\}
因为结束括号之间没有非JSON数据。
because there is no non-JSON data between closing brackets.
这篇关于正则表达式匹配嵌套的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!