简体   繁体   English

Django的查询集正则表达式提供额外的结果

[英]django query-set regex giving extra results

I am trying a make a regex query on django query set. 我正在尝试对Django查询集进行正则表达式查询。 I have a column having values like 我有一列具有像

'67,42,16', '16,42,67,70', '4,11,21,78', '12,45,6,22'

I want to extract columns having either '4' or '70' in them. 我想提取其中包含“ 4”或“ 70”的列。 Here is my regex 这是我的正则表达式

 _regex = r"(^|(\d*,)+)%s((,\d*)+|$)" %('4|70')

and my query 和我的查询

dict(Table.objects.filter(column__regex=_regex).values_list('row_id','column'))

Its returning the following result 其返回以下结果

{1563864L: u'67,42,16', 1563804L: u'16,42,67,70', 1563863L: u'4,11,21,78'}

I am expecting this result 1563804L: u'16,42,67,70', 1563863L: u'4,11,21,78' 我期望这个结果1563804L: u'16,42,67,70', 1563863L: u'4,11,21,78'

Not sure why I am getting 1563864L: u'67,42,16' as a result 不知道为什么我得到1563864L: u'67,42,16'结果是1563864L: u'67,42,16'

Any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance 提前致谢

I can only guess what you're trying to do, as it's always difficult to reverse a regular expression's original intent. 我只能猜测您要做什么,因为要逆转正则表达式的原始意图总是很困难。 Try this. 尝试这个。

_regex = '(^({0}),.+)|(.+,({0}),.+)|(.+,({0})$)'.format('4|70')

And a little more pythonic: 还有一点pythonic:

acceptable_values = [4, 70]
regex_template = '(^({0}),.+)|(.+,({0}),.+)|(.+,({0})$)'
_regex = regex_template.format('|'.join([str(v) for v in acceptable_values]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM