django queryset 过滤掉具有相同值的对象
django queryset filter out the objects with same values
我有
s = "[[[a_12]]] [[[|]]]"
我需要使用不应解析 [[[|]]]
的正则表达式从 s 中获取 a_12
。
我试过:
re.search(r'\[\[\[ [^\]]+ \]\]\]', s).group()
但如果我把它放在第一位,它也会抢 [[[|]]]
。我在正则表达式方面很糟糕,有人可以帮助我吗?
您可以通过将 |
添加到取反的 class
中来忽略 |
测试
>>> s = "[[[a_12]]] [[[|]]]"
>>> re.search(r'\[\[\[[^\]\|]+\]\]\]', s).group()
'[[[a_12]]]'
>>> s = "[[[|]]] [[[a_12]]]"
>>> re.search(r'\[\[\[[^\]\|]+\]\]\]', s).group()
'[[[a_12]]]'
[^\]\|]
匹配 |
或 ]
以外的任何内容
只捕获出现在[[[]]]
之间的单词字符。
>>> s = "[[[a_12]]] [[[|]]]"
>>> re.findall(r'\[\[\[(\w+)]]]', s)
['a_12']
我有
s = "[[[a_12]]] [[[|]]]"
我需要使用不应解析 [[[|]]]
的正则表达式从 s 中获取 a_12
。
我试过:
re.search(r'\[\[\[ [^\]]+ \]\]\]', s).group()
但如果我把它放在第一位,它也会抢 [[[|]]]
。我在正则表达式方面很糟糕,有人可以帮助我吗?
您可以通过将 |
添加到取反的 class
|
测试
>>> s = "[[[a_12]]] [[[|]]]"
>>> re.search(r'\[\[\[[^\]\|]+\]\]\]', s).group()
'[[[a_12]]]'
>>> s = "[[[|]]] [[[a_12]]]"
>>> re.search(r'\[\[\[[^\]\|]+\]\]\]', s).group()
'[[[a_12]]]'
[^\]\|]
匹配|
或]
以外的任何内容
只捕获出现在[[[]]]
之间的单词字符。
>>> s = "[[[a_12]]] [[[|]]]"
>>> re.findall(r'\[\[\[(\w+)]]]', s)
['a_12']