23

I'm looking to the first instance of a match two square brackets using regular expressions. Currently, I am doing

regex = re.compile("(?<=(\[\[)).*(?=\]\])")
r = regex.search(line)

which works for lines like

[[string]] 

returns string

but when I try it on a separate line:

[[string]] ([[string2]], [[string3]])

The result is

string]] ([[string2]], [[string3

What am I missing?

Rio
  • 14,182
  • 21
  • 67
  • 107

1 Answers1

51

Python *, +, ? and {n,m} quantifiers are greedy by default

Patterns quantified with the above quantifiers will match as much as they can by default. In your case, this means the first set of brackets and the last. In Python, you can make any quantifier non-greedy (or "lazy") by adding a ? after it. In your case, this would translate to .*? in the middle part of your expression.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Drew
  • 2,269
  • 21
  • 16
  • 14
    Minor nitpick. *regular expressions* are not fundamentally greedy or non-greedy. The python `re` package provides a greedy, recursive backtracking implementation of regular expressions. (+1 for right answer, though) – SingleNegationElimination Feb 22 '11 at 05:31