doniyor
doniyor

Reputation: 37896

django queryset filter out the objects with same values

I have

s = "[[[a_12]]] [[[|]]]"

I need to grab a_12 from s with regexp which should not parse [[[|]]].

I tried:

re.search(r'\[\[\[ [^\]]+ \]\]\]', s).group()

but it grabs [[[|]]] also if I put it into first place. I am so bad at regexp, can someone please help me?

Upvotes: 0

Views: 37

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

To capture only the word characters present inbetween [[[]]].

>>> s = "[[[a_12]]] [[[|]]]"
>>> re.findall(r'\[\[\[(\w+)]]]', s)
['a_12']

Upvotes: 1

nu11p01n73R
nu11p01n73R

Reputation: 26667

You can ignore | by adding | into the negated class

Test

>>> s = "[[[a_12]]] [[[|]]]"
>>> re.search(r'\[\[\[[^\]\|]+\]\]\]', s).group()
'[[[a_12]]]'

>>> s = "[[[|]]] [[[a_12]]]"
>>> re.search(r'\[\[\[[^\]\|]+\]\]\]', s).group()
'[[[a_12]]]'
  • [^\]\|] matches anything other than | or ]

Upvotes: 2

Related Questions