Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将url与python中的自定义模式进行比较_Python_Regex - Fatal编程技术网

将url与python中的自定义模式进行比较

将url与python中的自定义模式进行比较,python,regex,Python,Regex,当我在网站上搜索文章URL并获取所有”时 模式匹配:http://www.cnbc.com/(*)/(*)/(*)/(*)/(*)).html 因此,用(*)替换链接的任何可变部分的想法 问题是如何将链接匹配到模式?正则表达式(regex)匹配 你可以用一只手来做这件事 使用[^/]+而不是*,并转义点。前三个(*)部分是数字,因此可以使用[0-9]+。最后一个(*)节是字母和符号的组合,所以你可以使用 .+/COD>。我为那些不是程序员的用户制作了这样的模式,所以他们不能把URL转换成正则表

当我在网站上搜索文章URL并获取所有
”时

模式匹配:
http://www.cnbc.com/(*)/(*)/(*)/(*)/(*)).html

因此,用(
*
)替换链接的任何可变部分的想法

问题是如何将链接匹配到模式?

正则表达式(
regex
)匹配 你可以用一只手来做这件事



使用
[^/]+
而不是
*
,并转义点。前三个
(*)
部分是数字,因此可以使用
[0-9]+
。最后一个
(*)节是字母和符号的组合,所以你可以使用<代码> .+/COD>。我为那些不是程序员的用户制作了这样的模式,所以他们不能把URL转换成正则表达式,这只是一个例子,它与任何站点一起使用,你有什么代码,你尝试了什么?你应该考虑用<代码> +< /COD替换<代码> */Cube >。e> 因为在
/
除法器中不进行任何匹配是没有意义的。它工作得很好,我也可以用“\d”代替(.*),谢谢
import re

# Example url
url = 'http://www.cnbc.com/2016/03/13/financial-times-china-rebuts-economy-doomsayers-on-debt-and.html'
# Create a regex match pattern
pattern = r'http://www.cnbc.com/(.+)/(.+)/(.+)/(.+).html'
# Find match
m = re.match(pattern, url)
# Get Groups
m.groups()
('2016',
 '03',
 '13',
 'financial-times-china-rebuts-economy-doomsayers-on-debt-and')