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 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题