Jerry YY Rain
Jerry YY Rain

Reputation: 4382

How to handle regex string in JSON file with Python

I want to store some regex string in a json file, then use python to parse json file. however, as the regex backslash will be treated as invalid in json. so How should I do.

{
    "key" : "^https://www\.google\.com/search?.+",
    "value" : "google"
}

As you can see above, As regular expression, some fullstop (?.+) will be regex, some will be just fullstop (.), however, this json file will not be thought as valid. So how should I do.

Thanks!

Upvotes: 4

Views: 16741

Answers (3)

BlackPOP
BlackPOP

Reputation: 5737

Add one more back slash for each and every back slash .

        str=str.replace("\", "\\");

add above code into code before json usage..!

       {
          "key": "^https://www\\.google\\.com/search?.+",
          "value": "google"
       }

Its valid one..!

HOpe it helps...!

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

You need to use escape the backslashes for the regex, and then escape them again for the string processor:

>>> s = """{
...     "key" : "^https://www\\\\.google\\\\.com/search?.+",
...     "value" : "google"
... }"""
>>> json.loads(s)
{'key': '^https://www\\.google\\.com/search?.+', 'value': 'google'}

If you're going the other way, i. e. from a Python dictionary that contains a regex string to a JSON object, the encoder will handle this for you, but it's a good idea in general to use raw strings for regexes:

>>> s = """{
...     "key" : "^https://www\.google\.com/search?.+",
...     "value" : "google"
... }"""
>>> json.dumps(s)
'"{\\n    \\"key\\" : \\"^https://www\\\\.google\\\\.com/search?.+\\",\\n    \\"value\\" : \\"google\\"\\n}"'

Upvotes: 3

user2357112
user2357112

Reputation: 280778

You need to escape the backslashes when generating the JSON string. Whatever tool you use to generate the JSON probably has that built in. For example, with the Python json module:

>>> print json.dumps({'asdf': r'\s+'})
{"asdf": "\\s+"}

Note that the output has two backslashes in it. When parsed, those will become one backslash.

Upvotes: 1

Related Questions