Regex sre_constants.error: invalid character range

I am trying to get a JSON string from another string with this regex:

YAHOO.Finance.SymbolSuggest.ssCallback\((.*?)\)

It works on regex101.com , but when I use it in my code:

import re
import json
import requests

def stock_lookup(name):

    url = "http://autoc.finance.yahoo.com/autoc?query={0}&callback=YAHOO.Finance.SymbolSuggest.ssCallback".format(name)

    response = requets.get(url)

    json_data = json.loads(re.match(data,"YAHOO.Finance.SymbolSuggest.ssCallback\((.*?)\)"))


    return json_data

I get this error:

sre_constants.error: invalid character range

Thanks in advance

+4
source share
1 answer

You missed the arguments in the method match, the regular expression should appear first.

re.match("YAHOO.Finance.SymbolSuggest.ssCallback\((.*?)\)", data)

re.matchdocumentation :

re.match(pattern, string, flags=0)

The error is shown because, most likely, datathere is a range z-A.

+1
source

All Articles