如何在JSON字符串中查找单个ID?

时间:2019-04-16 21:59:00

标签: json regex

我有以下JSON输出,我必须检索仅与Client 3相关的ID。我需要一个返回该值的RegEx:

65cbae61-443d-5a1c-8a8e-d27073fb87af 

基于此输入:

"name":"Client 3"

JSON

[
  {"id":"2c061a53-a4af-4750-8db7-2915abb1bd24","name":"Client 1"},
  {"id":"944d74fa-8a35-455a-a2d3-7f55cb9288f8","name":"Client 2"},
  {"id":"65cbae61-443d-5a1c-8a8e-d27073fb87af","name":"Client 3"},
  {"id":"7943e6fc-917b-4a9c-abc5-a409ae2c7ff9","name":"Client 4"},
  {"id":"62d95289-8977-4acb-aab5-193dede7bdb0","name":"Client 5"}
]

我需要使用什么表达方式?

1 个答案:

答案 0 :(得分:1)

RegEx 1

This RegEx might help you to do so. It divides your input into four groups, where group three is your target string:

(.*)(:\")(.*)(\",\"name\":\"Client 3\")

enter image description here

RegEx 2

If you also wish to sanitize Client 3, you can simply use this RegEx and divide your input into five groups, where group three ($3) and group five ($5) are your target output and input.

enter image description here