Extract part of a regex match

前端 未结 9 1875
北海茫月
北海茫月 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: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 = '(.*)'
    # text = 'hello'
    if match := re.search(pattern, text, re.IGNORECASE):
      title = match.group(1)
    # hello
    

提交回复
热议问题