Search code examples
luaroblox

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


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.


Solution

  • 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