How to print text form a particular part of a string?

Viewed 235

I want to know how to get text form a particular part of a sentence this is what i mean. Hello (new text)., in the brackets it says new text and i want to find the text in the brackets and print only the text in the brackets.

2 Answers

The easiest way to do this is just to cancel out the Hello in your string.

local msg = "Hello (obama)"
msg = msg:gsub("Hello %(", ""):gsub("%)", "")

If the first part of the string is dynamic and is not always hello you can try this:

local msg = "Bye (obama)"
local pattern = "%p.+%p"
print(({string.match(msg, pattern):gsub("%)", ""):gsub("%(", "")})[1])

This soloution may not be the best and most efficient but it certainly works. I hope it helped you. Let me know if you have any questions.

Related