Match first instance of Python regex search

前端 未结 1 1545
天命终不由人
天命终不由人 2021-02-07 01:25

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

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


        
相关标签:
1条回答
  • 2021-02-07 02:15

    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.

    0 讨论(0)
提交回复
热议问题