Reputation: 911
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.
Upvotes: 2
Views: 883
Reputation: 5031
With the (
and )
surrounding the text, you can use string.match:
str = "Hello (new text)"
print(str:match("%((.+)%)"))
%((.+)%)")
is a pattern that captures every character between the (
and )
.
Resources: Lua 5.3 Reference Manual - 6.4.1 Patterns
Upvotes: 2
Reputation: 164
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.
Upvotes: 1