Search code examples
pythonpython-3.xsubdomain

there is a function in python i am not understanding


        link_regx = re.compile('<cite.*?>(.*?)<\/cite>')
        try:
            links_list = link_regx.findall(resp)
            for link in links_list:
                link = re.sub('<span.*>', '', link)

what is compile() and sub() function use here


Solution

  • re.compile(pattern, flags=0)

    Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search() and other methods.

    https://docs.python.org/3/library/re.html#re.compile

    re.sub(pattern, repl, string, count=0, flags=0)

    Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

    https://docs.python.org/3/library/re.html#re.sub