GuyC
GuyC

Reputation: 1019

a look-ahead regex?

I have this string - [1]{a,b} xxx [3] [4]{e,f}

I'd like to catch [1]{a,b} and [4]{e,f} and all other [.*]{.*}

When trying \[.*?\](?!\[)\{.*?\} I get [1]{a,b} and [3] [4]{e,f}

I'm using Python 2.7

What is wrong with the look-ahead? I specify not to include the [ character... Can it be solved without look-ahead?

Upvotes: 1

Views: 78

Answers (1)

Kasravnd
Kasravnd

Reputation: 107347

You need to use a negated character class, instead of .* :

>>> s= "[1]{a,b} xxx [3] [4]{e,f}"
>>> 
>>> import re

>>> re.findall(r'\[[^]]*\]\{[^}]*\}',s)
['[1]{a,b}', '[4]{e,f}']

Upvotes: 2

Related Questions