Skip to content
Advertisement

Python regular expression SyntaxError: unexpected character after line continuation character

I must have totally messed up my regular expression. I’m trying to find the ID and value in the following str (and eventually will need to find description and it’s value too):

 "cve": {
        "CVE_data_meta": {
            "ASSIGNER": "cve@mitre.org",
            "ID": "CVE-2020-1785"
        },
        "data_format": "MITRE",
        "data_type": "CVE",
        "data_version": "4.0",
        "description": {
            "description_data": [
                {
                    "lang": "en",
                    "value": "Mate 10 Pro;Honor V10;Honor 10;Nova 4 smartphones have a denial of service vulnerability. The system does not properly check the status of certain module during certain operations, an attacker should trick the user into installing a malicious application, successful exploit could cause reboot of the smartphone."
                }
            ]
        },

But for some reason, I get the above error message.

This is my code:

m= re.match("ID"s*"([A-Za-z0-9-]*)"", str)
if m:
    print (m.groups())

I tested in https://pythex.org/ and the regular expression of

ID”: “([A-Za-z0-9-]*)”

worked, but running it in the program has issues with the semicolon so I tried to replace it with s* but it’s having issues with line continuation. Any ideas? Right now I need to just capture the ID, and then I’ll need the description.

Hope you can help! I’m new to python and don’t do regular expressions that often.

Advertisement

Answer

You’ve closed the string after “ID”, and the backslash right after that is (outside a string) the line continuation character. The error is that after a line continuation, you need a newline, but of course that’s not what you want at all.

I think you can resolve this by just converting your outer quotes to single quotes, like 'ID"s*"([A-Za-z0-9-]*)"'

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement