Python 带有正则表达式的 Django 过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25803822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:40:21  来源:igfitidea点击:

Django filter with regex

pythonregexdjango

提问by Mugdha

I want to write regex for django's model's charField. That regex contains all letters and last character contains "/".

我想为 django 模型的 charField 编写正则表达式。该正则表达式包含所有字母,最后一个字符包含“/”。

Eg: "sequences/"

例如:“序列/”

I return regex as follows,

我返回正则表达式如下,

Model.objects.filter(location_path__iregex=r'^[a-zA-Z]/$')

It is not showing filter data. demo data for location_path is ['sequences/', 'abc/xyz/', 'abc/aaaa/aaa', 'pqr/']

它没有显示过滤器数据。location_path 的演示数据是 ['sequences/', 'abc/xyz/', 'abc/aaaa/aaa', 'pqr/']

I want to filter data like 'sequences/', 'pqr/' which contains any character from a-z and field end with '/'

我想过滤数据,如 'sequences/'、'pqr/',其中包含 az 中的任何字符,字段以 '/' 结尾

Please give me the proper regex pattern and syntax

请给我正确的正则表达式模式和语法

回答by Avinash Raj

You need to add +after the character class, so that it would match one or more letters,

您需要+在字符类之后添加,以便匹配一个或多个字母,

r'^[a-zA-Z]+/$'