Search code examples
luacoronasdklua-patterns

Matching strings with multiple words between two words


In my program, I am trying match a string that has two letters and then a few words between them like such: "! hello my name !" In this example, the string "hello my name" can change in the number of words to a string such as: "hello" or even more words. Anyways, how can I match the string between the exclamation marks? The main problem is that I cannot figure out the expression to use in the string match to represent a string with multiple words of an unknown length.


Solution

  • Use the pattern !([^!]+)!, in which [^!]* matches zero or more characters that aren't !.

    print(string.match("! hello my name !","!([^!]*)!"))