doulu2011 2016-08-30 07:59
浏览 64
已采纳

匹配可能看起来像JSON的字符串

I have expression parser and I want to add json support so I need to match one of [ or { any number of characters that may include paired [] and {} and then ] or }.

So far I have ([[{])(.(?!\1))*[\]}] it match [foo} but I can live with that. Invalid json will be catch when json_decode return null.

I need to match JSON in strings like this:

{"foo":"bar"} == 20
[1,2,3,4] == 10

but also first JSON in those strings:

{"foo": "bar"}["foo"]
[1,[2],{"foo":"bar"},4][0]
{"foo": "bar"} == {"foo": "bar"}

So far I have regex like this ^([\[{](?>"(?:[^"]|\\")*"|[^\]}]|(?1))*[\]}]): demo but it don't match: [1,[2],{"foo":"bar"},4]

  • 写回答

1条回答 默认 最新

  • douyamitong57935 2016-08-30 08:58
    关注

    This is matching regex I found based on S-Expression regex from comment in this answer Extract s-expressions from a beginning of the string:

    ^([\[{](?>"(?:[^"]|\\")*"|[^[{\]}]|(?1))*[\]}])
    

    https://regex101.com/r/cUkoVe/2

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突