2

I need a regex that will match the following line:

{"httpStatus": "OK", "payload": {"status": "OK"}, "httpCode": 200}

but not

{"httpStatus": "OK", "payload": {"status": "OK", "config": {}}, "httpCode": 200}

So basically it should match the string if it not contains config

For checking if the string contains status the regex is:

(?=\"status\": \"OK\")
alexm92
  • 386
  • 3
  • 15

3 Answers3

3

Parse the input as JSON using the json module:

import json

s = """{"httpStatus": "OK", "payload": {"status": "OK"}, "httpCode": 200}"""
j = json.loads(s)
if "config" not in j:
    print("match")
1

If you need to use regex you can use the following negative look ahead based regex :

^((?!"config").)*$

Demo

Along side the good notes by @Jerry and @nhahtdh You may note that this regex doesn't consider the type of words and i match the dictionaries that has config in values.(you can see the detail in demo) as a better solution you can use json module.

The following recursion function will do the task for any nested dictionary :

>>> def checker(s,val):
...    for k in s:
...        if k==val:
...           return False
...        elif isinstance(s[k],dict):
...           return checker(s[k],val)
...    return True
... 
>>> 
>>> s="""{"httpStatus": "OK", "payload": {"status": "OK"}, "httpCode": 200}"""
>>> js=json.loads(s)
>>> checker(js,'config')
True

>>> s="""{"httpStatus": "OK", "payload": {"status": "OK", "config": {}}, "httpCode": 200}"""
>>> js=json.loads(s)
>>> checker(js,'config')
False

And a nested dictionary :

>>> s="""{"httpStatus": "OK", "payload": {"status": "OK", "sts":{"nested":{"config": {}}}}, "httpCode": 200}"""
>>> 
>>> js=json.loads(s)
>>> checker(js,'config')
False
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • 3
    @alexm Just as a side note, this method would be one of the slowest out there, even in the 'regex realm'. Also, the above would avoid a match if the string contains a word containing `config`, for example `nonconfig` or `sideconfigurations` and the likes. – Jerry Apr 29 '15 at 09:36
  • 3
    This is a terrible solution. There is no differentiation between data and key here (apart from what Jerry has mentioned about wrong keys). – nhahtdh Apr 29 '15 at 09:48
  • @alexm Checkout the edit! – Mazdak Apr 29 '15 at 10:47
  • @Jerry Yes you are right i edit the answer .thanks for reminding and attention! – – Mazdak Apr 29 '15 at 10:48
0

Do all the other fields have to be there in that exact form, or does it suffice that it does not contain "config"?

import re
n = str({"httpStatus": "OK", "payload": {"status": "OK", "config": {}}, "httpCode": 200})
print len(re.findall("config", n)) == 0
EvenLisle
  • 4,672
  • 3
  • 24
  • 47