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").)*$
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
Post a Comment