Jump to content




get text in brackets


  • You cannot reply to this topic
4 replies to this topic

#1 FuuuAInfiniteLoop(F.A.I.L)

  • Banned
  • 435 posts
  • LocationThe left part of this post

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]

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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]


#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 30 March 2013 - 01:04 AM

pattern of
'%[(.-)%]'
works too. Curious to know if there's a difference though.

#4 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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".

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users