python - 如何构造正则表达式来检查有效路径?
问题描述
我正在尝试为下面的路径构建正则表达式。你能帮我为它构建正则表达式吗?
path = "root-dir/document/2018/02/29/14/02-sample.txt"
pattern = '((a-z)+(-)(a-z)+/(a-z)+/(\d{4})/(\d{2})/(\d{2})/(\d{2})/(\d{2)(-)(a-z)+(.)(a-z)+)'
bool(re.match(pattern,path))
"root-dir/document/2015/01/25/13/01-sample.txt" //this should be accepted
"root-dir/2015/01/25/13/01-sample.txt" //this should not be accepted
"root-dir/document/201/01/25/13/01-sample.txt" //this should not be accepted as 201 part should be 4 digit
"root-dir/document/2015/01/2/13/01-sample.txt" //this should be not accepted as 2 part should be 2 digit
"root-dir/document/2015/01/25/13/sample.txt" //this should not be accepted as the last part should be something like this 03-sample.txt
解决方案
您的模式几乎没有问题,正确的用法是:
import re
pattern = r'([a-z]+(-)[a-z]+/[a-z]+/(\d{4})/(\d{2})/(\d{2})/(\d{2})/(\d{2})(-)[a-z]+(\.)[a-z]+)'
print(bool(re.match(pattern,path))) #True
您的模式'((a-z)+(-)(a-z)+/(a-z)+/(\d{4})/(\d{2})/(\d{2})/(\d{2})/(\d{2)(-)(a-z)+(.)(a-z)+)'
不起作用,因为:
\d{2)
-你应该关闭{
not}
)
a-z
- 这相当于里面的任何小写字母,而且只有在里面[
]
不是(
)
.
\.
-如果你的意思是字符.
(ASCII:46),你应该使用它,因为re
它表示任何不是换行符的字符,还记得使用原始字符串而不是普通字符串
推荐阅读
- plot - scilab 在同一张图上绘制函数和数据集
- php - 使用函数获取数据时数据库行无限重复
- javascript - 创建新用户时,仪表板上没有显示
- python - 如何将 ArrayField 定义为 django 表单
- go - 如何理解以下代码中的“ch=ch1”?
- c# - 如果至少检查了一个子节点,则检查父节点
- hugo - 如何使用 Hugo 创建三步嵌套?
- woocommerce - Noindex特定类别分页页面
- cakephp - CakePHP 3.x - 文件上传导致令牌不匹配
- javascript - 自定义元素类型在脚本类型 = 模块中加载时报告 HTMLElement?