Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/65878340/how-to-print-text-form-a-particular-part-of-a-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...