正则表达式以避免完全匹配

时间:2016-07-11 12:07:33

标签: regex jmeter

我想提取id所在对象(例如地区和国家/地区)的typeavailable=true。我使用正则表达式{"name":".+?","id":"(.+?)","type":"(.+?)","available":true

我从名称开始得到一个完整的行... {中间的其他内容} ... available = true 其中包括其他ID /名称,即使available=false为{好。

以下示例数据

{  
 "error":null,
 "data":{  
   "airports":[],
   "pocs":[],
   "regions":[  
     {  
        "name":"Central America",
        "id":"L04305",
        "type":"CruiseArea",
        "available":false,
        "countries":"Mexico",
        "group":null
     },
     {  
        "name":"Caribbean",
        "id":"L04304",
        "type":"CruiseArea",
        "available":false,
        "countries":"St Maarten,Barbados,British Virgin Islands,St Kitts and Nevis,St Vincent and the Grenadines,Antigua",
        "group":null
     },
     {  
        "name":"Western Mediterranean",
        "id":"L34381",
        "type":"CruiseArea",
        "available":true,
        "countries":"Spain",
        "group":null
     },
     {  
        "name":"Eastern Mediterranean",
        "id":"L34373",
        "type":"CruiseArea",
        "available":false,
        "countries":"Greece,Italy,Slovenia,Montenegro,Croatia",
        "group":null
     },
     {  
        "name":"North Africa And Middle East",
        "id":"L04301",
        "type":"CruiseArea",
        "available":false,
        "countries":"Morocco",
        "group":null
     },
     {  
        "name":"Fjords, Iceland And Arctic",
        "id":"L34384",
        "type":"CruiseArea",
        "available":true,
        "countries":"Norway",
        "group":null
     },
     {  
        "name":"Northern Europe And UK",
        "id":"L34383",
        "type":"CruiseArea",
        "available":true,
        "countries":"United Kingdom",
        "group":null
     }
  ],
  "countries":[  
     {  
        "name":"Spain",
        "id":"ESP",
        "type":"COUNTRY",
        "available":true,
        "countries":null,
        "group":null
     },
     {  
        "name":"Jamaica",
        "id":"JAM",
        "type":"COUNTRY",
        "available":false,
        "countries":null,
        "group":null
     },
     {  
        "name":"Greece",
        "id":"GRC",
        "type":"COUNTRY",
        "available":false,
        "countries":null,
        "group":null
     },
     {  
        "name":"Italy",
        "id":"ITA",
        "type":"COUNTRY",
        "available":false,
        "countries":null,
        "group":null
     },
     {  
        "name":"Turkey",
        "id":"TUR",
        "type":"COUNTRY",
        "available":false,
        "countries":null,
        "group":null
     },
     {  
        "name":"Barbados",
        "id":"BRB",
        "type":"COUNTRY",
        "available":false,
        "countries":null,
        "group":null
     },
     {  
        "name":"Mexico",
        "id":"MEX",
        "type":"COUNTRY",
        "available":false,
        "countries":null,
        "group":null
     },
     {  
        "name":"Israel",
        "id":"ISR",
        "type":"COUNTRY",
        "available":false,
        "countries":null,
        "group":null
     },
     {  
        "name":"Cuba",
        "id":"CUB",
        "type":"COUNTRY",
        "available":false,
        "countries":null,
        "group":null
     },
     {  
        "name":"Croatia",
        "id":"HRV",
        "type":"COUNTRY",
        "available":false,
        "countries":null,
        "group":null
     }
    ],
    "itineraries":[],
    "airportGroups":[]
  },
 "nomatch":false,
 "durations":null,
 "searchError":null
}

2 个答案:

答案 0 :(得分:0)

最简单的解决方案(没有完全重写)可能是

{"name":"[^"]*","id":"([^"]*)","type":"([^"]*)","available":true,[^{}]*}

而不是扫描. - 除换行符之外的任何字符 - 扫描除{} - [^{}]之外的任何字符。

See it here at regex101

答案 1 :(得分:0)

我建议从JMeter 3.0版本中获取JSON Path PostProcessor,因为它更适合使用JSON数据。

相关的JSON路径表达式将是:

  • $..[?(@.available == true)].id
  • $..[?(@.available == true)].type

演示:

JSON Path PostProcessor Demo

参考文献: