Extract part of a regex match

前端 未结 9 1879
北海茫月
北海茫月 2020-11-22 13:01

I want a regular expression to extract the title from a HTML page. Currently I have this:

title = re.search(\'.*\', html, re.IGNOR         


        
相关标签:
9条回答
  • 2020-11-22 13:43

    Use ( ) in regexp and group(1) in python to retrieve the captured string (re.search will return None if it doesn't find the result, so don't use group() directly):

    title_search = re.search('<title>(.*)</title>', html, re.IGNORECASE)
    
    if title_search:
        title = title_search.group(1)
    
    0 讨论(0)
  • 2020-11-22 13:45

    Try:

    title = re.search('<title>(.*)</title>', html, re.IGNORECASE).group(1)
    
    0 讨论(0)
  • 2020-11-22 13:54
    re.search('<title>(.*)</title>', s, re.IGNORECASE).group(1)
    
    0 讨论(0)
  • 2020-11-22 13:56

    I'd think this should suffice:

    #!python
    import re
    pattern = re.compile(r'<title>([^<]*)</title>', re.MULTILINE|re.IGNORECASE)
    pattern.search(text)
    

    ... assuming that your text (HTML) is in a variable named "text."

    This also assumes that there are not other HTML tags which can be legally embedded inside of an HTML TITLE tag and no way to legally embed any other < character within such a container/block.

    However ...

    Don't use regular expressions for HTML parsing in Python. Use an HTML parser! (Unless you're going to write a full parser, which would be a of extra work when various HTML, SGML and XML parsers are already in the standard libraries.

    If your handling "real world" tag soup HTML (which is frequently non-conforming to any SGML/XML validator) then use the BeautifulSoup package. It isn't in the standard libraries (yet) but is wide recommended for this purpose.

    Another option is: lxml ... which is written for properly structured (standards conformant) HTML. But it has an option to fallback to using BeautifulSoup as a parser: ElementSoup.

    0 讨论(0)
  • 2020-11-22 13:57

    Note that starting Python 3.8, and the introduction of assignment expressions (PEP 572) (:= operator), it's possible to improve a bit on Krzysztof Krasoń's solution by capturing the match result directly within the if condition as a variable and re-use it in the condition's body:

    # pattern = '<title>(.*)</title>'
    # text = '<title>hello</title>'
    if match := re.search(pattern, text, re.IGNORECASE):
      title = match.group(1)
    # hello
    
    0 讨论(0)
  • 2020-11-22 14:00

    The provided pieces of code do not cope with Exceptions May I suggest

    getattr(re.search(r"<title>(.*)</title>", s, re.IGNORECASE), 'groups', lambda:[u""])()[0]
    

    This returns an empty string by default if the pattern has not been found, or the first match.

    0 讨论(0)
提交回复
热议问题