Reputation: 37896
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
Reputation: 174706
To capture only the word characters present inbetween [[[]]]
.
>>> s = "[[[a_12]]] [[[|]]]"
>>> re.findall(r'\[\[\[(\w+)]]]', s)
['a_12']
Upvotes: 1
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