推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
zasilla
V2EX  ›  Python

正则匹配 url 最后的文件名如何匹配

  •  
  •   zasilla · Dec 13, 2018 · 4563 views
    This topic created in 2888 days ago, the information mentioned may be changed or developed.
    例如:例如: https://cn.bing.com/az/hprichbg/rb/SphinxObservatory_ZH-CN7733546261_1920x1080.jpg
    目标匹
    目标匹配:SphinxObservatory_ZH-CN7733546261_1920x1080.jpg

    我是这样匹配的:r'/(.*?.jpg)'
    结果匹配的是:/cn.bing.com/az/hprichbg/rb/SphinxObservatory_ZH-CN7733546261_1920x1080.jpg
    查了一下非贪婪匹配的问题,加不加“?”结果都一样,谁能指导一下
    15 replies  •  2018-12-14 11:18:24 +08:00
    5200
    5200
    Dec 13, 2018❤️2
    1
    ```
    (?<=/)[^/]+?jpg
    ```
    图片后缀都是 jpg 吗?这样呢
    LuJason
    LuJason
    Dec 13, 2018
    2
    os.path.basename(url) (逃....
    LuJason
    LuJason
    Dec 13, 2018
    3
    os.path.basename(url)

    (逃....
    leoleoasd
    leoleoasd
    Dec 13, 2018
    4
    .split("//")[-1]
    leoleoasd
    leoleoasd
    Dec 13, 2018
    5
    .split(r"/")[-1]
    CloudMx
    CloudMx
    Dec 13, 2018
    6
    [^\/]+\.\w+$

    ??
    CloudMx
    CloudMx
    Dec 13, 2018❤️1
    7
    [^\/]+$
    ????
    zasilla
    zasilla
    OP
    Dec 13, 2018
    8
    都能匹配到,看来是我思路不对了
    reus
    reus
    Dec 13, 2018
    9
    为什么要用正则?
    zasilla
    zasilla
    OP
    Dec 13, 2018
    10
    @reus 自己不熟悉,正则学习的最早,就最先想到了,@leoleoasd 的方法好像最简单
    5200
    5200
    Dec 13, 2018
    11
    @zasilla
    看你是不是从大批量网址里面匹配了,
    如果只是一条记录,感觉就没必要用正则了。。。
    如果是匹配某个网页里面的全部图片名还是比较有效的。
    metamask
    metamask
    Dec 13, 2018
    12
    (?!.*/).*\.jpg
    lihongjie0209
    lihongjie0209
    Dec 13, 2018
    13
    为什么不用 url 对象或者是 path 对象
    xueyoucai
    xueyoucai
    Dec 14, 2018
    14
    .split(r"/")[-1]
    br00k
    br00k
    Dec 14, 2018
    15
    [^\/]+$
    😂
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2311 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 13:24 · PVG 21:24 · LAX 05:24 · JFK 08:24
    ♥ Do have faith in what you're doing.