Python regex not working

前端 未结 2 1865
盖世英雄少女心
盖世英雄少女心 2021-01-04 12:32

I am using the following code:

downloadlink = re.findall(\"http://uploadir.com/u/(.*)\\b\", str(downloadhtml))

However, when I pass it the

2条回答
  •  我在风中等你
    2021-01-04 13:35

    Get in the habit of making all regex patterns with raw strings:

    In [16]: re.findall("http://uploadir.com/u/(.*)\b", '')
    Out[16]: []
    
    In [17]: re.findall(r"http://uploadir.com/u/(.*)\b", '')
    Out[17]: ['bb41c5b3']
    

    The difference is due to \b being interpreted differently:

    In [18]: '\b'
    Out[18]: '\x08'
    
    In [19]: r'\b'
    Out[19]: '\\b'
    

    '\b' is an ASCII Backspace, while r'\b' is a string composed of the two characters, a backslash and a b.

提交回复
热议问题