【问题标题】:Why does the regex not capture 'www.'为什么正则表达式不捕获“www”。
【发布时间】:2016-06-04 15:14:58
【问题描述】:

我正在创建一个简单的(我认为这很简单)正则表达式来分组捕获 ulr 信息。除非我使用带有“www”的网址,否则一切都排成一行。

表达式:

((https?):\/\/(?:www\.)?([\w\.\-\:]+)\/(.+))

测试网址:

http://11.111.111.1:1010/nexus-2.3.1/service/local/artifact/maven/content?r=fake_release&g=com.fake&a=com.rake.fake.soap.webapp&v=LATEST&e=war
https://hello-ci.fake-re.com/jenkins/view/RAS/job/RAS_Designtime_Master/site/com.rake.fake.ras.documentation/kwl/Assessment-faker-gage.html
https://regex101.com/#python
https://www.google.com
http://www.apple.com

为什么我在https://www.google.comhttp://www.apple.com 上都找不到匹配项

注意:此正则表达式适用于 python 应用程序

【问题讨论】:

  • 仅供参考,您可能想使用urlparse 而不是正则表达式。

标签: python regex url


【解决方案1】:

由于必须使用/,这些 URL 不匹配。使用非捕获组和? 量词使该部分成为可选:

((https?):\/\/(?:www\.)?([\w\.\-\:]+)(?:\/(.+))?)
                                     ^^^      ^^

regex demo

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2020-08-11
    相关资源
    最近更新 更多