python - Regex substring not in string -


this question has answer here:

i need regex match following line:

{"httpstatus": "ok", "payload": {"status": "ok"}, "httpcode": 200} 

but not

{"httpstatus": "ok", "payload": {"status": "ok", "config": {}}, "httpcode": 200} 

so should match string if not contains config

for checking if string contains status regex is:

(?=\"status\": \"ok\") 

if need use regex can use following negative ahead based regex :

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

demo

along side notes @jerry , @nhahtdh may note regex doesn't consider type of words , match dictionaries has config in values.(you can see detail in demo) better solution can use json module.

the following recursion function task nested dictionary :

>>> def checker(s,val): ...    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 nested dictionary :

>>> s="""{"httpstatus": "ok", "payload": {"status": "ok", "sts":{"nested":{"config": {}}}}, "httpcode": 200}""" >>>  >>> js=json.loads(s) >>> checker(js,'config') false 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -