Search code examples
pythonregexluacapture

What is the regex expression to find everything between --[[ and ]]?


I'm trying to clean up some Lua code files using a Python script and regex by removing comments. I'm using the following regular regular expression to find multiline comments: "--\[\[[^]\]]+"

For example:

--[[ This is a comment
on multiple lines
that needs to be removed ]]

The expression picks these up without a problem. However, there are also comments like this:

      --[[
      if thing == "whatever" or thing == "whateeeever" then
        self:print( ">" .. thing.. params[2] .. " something " )
      -- printing the thing
         ]]

On a comment like this, the regex only captures until the first ] on the end of params[2] instead of all the way to ]]

Can anyone provide me a working regex that captures everything, including square brackets?


Solution

  • You might try a pattern like

    ^[^\S\n]*--\[\[.*(?:\n(?!.*\]\]).*)*\n.*\]\]
    

    Regex demo