This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FuuuAInfiniteLoop(F.A.I.L)'s profile picture

get text in brackets

Started by FuuuAInfiniteLoop(F.A.I.L), 29 March 2013 - 01:40 PM
FuuuAInfiniteLoop(F.A.I.L) #1
Posted 29 March 2013 - 02:40 PM
I want to know how do i obtain the text inside brackets

ex: [lol] will return lol and [[fdfsdfsdf]] will return [fdfsdfsdf]
Kingdaro #2
Posted 29 March 2013 - 03:06 PM
Use string.match.


local matchstr = '%[(.-%]*)%]'
local text

text = string.match('[some brackets]', matchstr)
print(text) --> some brackets

text = string.match('[[some more brackets]]', matchstr)
print(text) --> [some more brackets]
remiX #3
Posted 30 March 2013 - 01:04 AM
pattern of
'%[(.-)%]'
works too. Curious to know if there's a difference though.
Kingdaro #4
Posted 30 March 2013 - 05:24 AM
I actually tested both in my lua prompt on my computer before making my post up there, and the matchstring you suggested stops capturing the string after the first right bracket. In the case of "[[some more brackets]]", it would capture "[some more brackets".
remiX #5
Posted 30 March 2013 - 05:52 AM
Oh dang it. Didn't read the last part of the OP properly :P/>
Thought he would only use strings with single brackets.