相关疑难解决方法(0)

正则表达式匹配不包含单词的行?

我知道可以匹配一个单词,然后使用其他工具(例如grep -v)反转匹配.但是,我想知道是否可以使用正则表达式匹配包含特定单词的行(例如hede).

输入:

hoho
hihi
haha
hede
Run Code Online (Sandbox Code Playgroud)

码:

grep "<Regex for 'doesn't contain hede'>" input
Run Code Online (Sandbox Code Playgroud)

期望的输出:

hoho
hihi
haha
Run Code Online (Sandbox Code Playgroud)

regex regex-negation

4121
推荐指数
27
解决办法
316万
查看次数

如何在Django查询集过滤中做不相等的操作?

在Django模型QuerySets中,我看到有一个__gt__ltfor的比较值,但是有__ne/ !=/ <>(不等于?)

我想用不等于过滤掉:

例:

Model:
    bool a;
    int x;
Run Code Online (Sandbox Code Playgroud)

我想要

results = Model.objects.exclude(a=true, x!=5)
Run Code Online (Sandbox Code Playgroud)

!=不正确的语法.我试过__ne,<>.

我最终使用:

results = Model.objects.exclude(a=true, x__lt=5).exclude(a=true, x__gt=5)
Run Code Online (Sandbox Code Playgroud)

python django django-models django-queryset

608
推荐指数
15
解决办法
34万
查看次数

#1139 - 从regexp获得错误'重复 - 操作数操作数无效'

我在使用正则表达式从MySQL表中选择一些结果时遇到了麻烦.

我正在使用此查询

SELECT text 
FROM `articles` 
WHERE content REGEXP '.*<img.*?src=\"http://www' 
ORDER BY date DESC
Run Code Online (Sandbox Code Playgroud)

它说

#1139 - Got error 'repetition-operator operand invalid' from regexp
Run Code Online (Sandbox Code Playgroud)

我使用Notepad ++测试了正则表达式并且它有效,为什么MySQL给我这个错误,我该如何解决它?

regex mysql select

19
推荐指数
1
解决办法
3万
查看次数

将过滤器组合到Tastypie中的一个查询中

可能重复:
Django Tastypie高级过滤:如何使用Q对象进行复杂查找

我有一个tastypie modelRseource,看起来像这样:

class TaggedResource(ModelResource):
    tags = ListField()
    user = fields.ForeignKey(UserProfileResource, 'user')

    class Meta:
        queryset = Media.objects.all().order_by('-timestamp')
        authorization = MediaAuthorization()
        detail_allowed_methods = ['get', 'post', 'put', 'delete','patch']

    filtering = {
        #'user': ALL_WITH_RELATIONS,
        #exact is date, lt is less than lte less than equal to, etc
        'timestamp': ['exact', 'range', 'lt', 'lte', 'gte', 'gt'],
        'social_source': ALL,
        'media_type': ALL,
        'comment': ['exact', 'startswith', 'endswith', 'contains'],
        'media_text': ['exact', 'startswith', 'endswith', 'contains'],
    }
Run Code Online (Sandbox Code Playgroud)

我需要在过滤器之间使用OR运算符,并且希望将查询合并到一个参数中.例如,我想从注释字段OR media_text字段返回包含单词"test"过滤的对象.

这将是理想的:http:mysite.com/api/v1/tagged?q = test

其中'q'对两个字段执行OR过滤.

这可行吗?

更新:这是我正在使用的高级过滤器,但我不确定如何获得一个OR语句:

def build_filters(self, filters=None):
    if …
Run Code Online (Sandbox Code Playgroud)

django tastypie

5
推荐指数
1
解决办法
5572
查看次数