带正则表达式的Django过滤器 [英] Django filter with regex

查看:586
本文介绍了带正则表达式的Django过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如:sequences /



I返回正则表达式如下,

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

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



我想过滤像'sequences /','pqr /'这样的数据,其中包含来自az和field的任何字符'/'



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

解决方案

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

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


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

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/']

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

Please give me the proper regex pattern and syntax

解决方案

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

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

这篇关于带正则表达式的Django过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆