【问题标题】:Regex for get words After matching words匹配单词后获取单词的正则表达式
【发布时间】:2017-06-01 04:39:14
【问题描述】:

在下面的内容中,我需要正则表达式来获取匹配后的提交内容值 "commits": 请建议正则表达式模式以实现这一目标。

"{"commit":{"id":"f0180cb5d2f71906bc3875f3526a85c73cd4bf35","short_id":"f0180cb5","title":"更新 Jenkinsfile","author_name":"Administrator","author_email":"abc@ mail.com","created_at":"2017-05-31T07:15:59.000+00:00","message":"更新 Jenkinsfile"},"commits":[{"id":"f0180cb5d2f71906bc3875f3526a85c73cd4bf35 ","short_id":"f0180cb5","title":"更新 Jenkinsfile","author_name":"Administrator","author_email":"admin@mail.com","created_at":"2017-05-31T07: 15:59.000+00:00","message":"更新 Jenkinsfile"}],"diffs":[{"old_path":"Jenkinsfile","new_path":"Jenkinsfile","a_mode": "100644","b_mode":"100644","diff":"--- a/Jenkinsfile\n+++ b/Jenkinsfile\n@@ -2,6 +2,7 @@ node('Infrastructure')\n { \n \n \n+//test\n stage 'Checkout' \n try\n {\t\n","new_file":false,"renamed_file":false,"deleted_file":false}],"compare_timeout ":false,"compare_same_ref":false}"

【问题讨论】:

  • 你应该展示你已经尝试过的东西
  • 这不只是一个 JSON 块吗?所有提交数据将位于 ["commits"][0]["id"]、["commits"][0]["short_id"] 等处。尝试使用类似jsonviewer.stack.hu 的内容以更友好的方式查看它格式。我不知道你为什么要尝试正则表达式 JSON 对象。
  • 确实是 JSON,@kali 你用的是什么语言?在使用正则表达式之前尝试解析/解码 JSON。

标签: regex regex-greedy regex-group


【解决方案1】:

此正则表达式将捕获"commits": 的数组值。

请注意,要使其正常工作,"commits" 的值必须是对象数组(以 [{ 开头并以 }] 结尾。

我赞同 William Cross 的观点,你为什么要对 JSON 进行正则表达式。解析它以访问commits 的值会更安全。

查看工作中的regex example here

/"commits":(\[\{.*?\}\])/g

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多