Checking if a string has brackets in them

So, I’m making a admin panel, and I have to check if brackets are in them. Here’s an example:
[1DAY] Flood/Caps
I’d have to check if there are brackets in that message, and i need it to return 1DAY then, regardless of the message size.
Please tell me how to do this, thanks, Axolix

1 Like
local brackets = string.match(str, "%[.+%]")
if brackets then
    return brackets:sub(2, -2)

Edit: fixed if check


There are some useful string comparing functions found here:

While the current solution works, there’s a pattern exactly for this kind of purpose, %b. As shown on the lua site, (bottom of page) %b[] matches characters between brackets. (You can do %bxy and it will match anything between x and y, but brackets and parentheses are the most common use.)


I think that the question was asking how to see if the string contains any brackets, not how to get text within brackets.

Correct me if I’m wrong


It sounds like he wants the text within the brackets, although I could be wrong. It’ll also return nil if there are no matching pairs of brackets, which would show when there are none.