Lua extracting string between two parantheses

Another item in a pattern is the ‘%b’, that matches balanced strings. Such item is written as ‘%bxy’, where x and y are any two distinct characters; the x acts as an opening character and the y as the closing one. For instance, the pattern ‘%b()’ matches parts of the string that start with a `(´ and finish at the respective `)´:


    print(string.gsub("a (enclosed (in) parentheses) line",
                      "%b()", ""))
      --> a  line

Typically, this pattern is used as ‘%b()’, ‘%b[]’, ‘%b%{%}’, or ‘%b’, but you can use any characters as delimiters.

Leave a Reply

Your email address will not be published. Required fields are marked *