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 求Houdini使用行家,付费。价格面议。
  • ¥15 AttributeError: 'EasyDict' object has no attribute 'BACKUP_DB_INFO'
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 CF1927D 求Hack