为什么正则表达式不能捕获'www。'

时间:2016-02-23 00:11:31

标签: python regex url

我正在创建一个简单的(我认为这很简单)正则表达式来捕获组中的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应用程序

1 个答案:

答案 0 :(得分:4)

由于强制性/,这些网址不匹配。使用非捕获组和?量词:

使该部分可选
((https?):\/\/(?:www\.)?([\w\.\-\:]+)(?:\/(.+))?)
                                     ^^^      ^^

请参阅regex demo