h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
582 stars 85 forks source link

Support regular expression contains instead of 'a in b' #40

Closed memborsky closed 4 years ago

memborsky commented 4 years ago

Use re.search() to check if the value being compared is contained within the filter field.

>>> from jsonpath_ng.ext import parse
>>> 
>>> simple_dict = {
...   "phone_numbers": [
...     {
...       "type"  : "iPhone",
...       "number": "0123-4567-8888"
...     },
...     {
...       "type"  : "home",
...       "number": "0123-4567-8910"
...     },
...     {
...       "type"  : "home",
...       "number": "1098-7654-3210"
...     }
...   ]
... }
>>> 
>>> expression = parse('$.phone_numbers[?(@.type =~ "^hom")]')
>>> 
>>> [x.value for x in expression.find(simple_dict)]
[{'type': 'home', 'number': '0123-4567-8910'}, {'type': 'home', 'number': '1098-7654-3210'}]
>>> 
>>> expression = parse('$.phone_numbers[?(@.number =~ "[0-9]{4}-4567-[0-9]{4}")]')
>>> 
>>> [x.value for x in expression.find(simple_dict)]
[{'type': 'iPhone', 'number': '0123-4567-8888'}, {'type': 'home', 'number': '0123-4567-8910'}]
coveralls commented 4 years ago

Coverage Status

Coverage remained the same at 0.0% when pulling 7ff235a198ef1bc61ed7846bf621aaad70528a9d on memborsky:add-regular-expression-contains-support into d4b8b87407a298a48e5229be9b9fb517fb539b2f on h2non:master.