Search code examples
lua

How can I write the pattern correctly in string.gmatch?


I wrote some code like this:

        local word = "<color=#FF0000>123</color>asdf<color=#00FF00>456</color>"
        local list = {}
        for match in string.gmatch(word, "<color=#%w+>.+</color>") do
            print(match)
        end

But the output was the total word "<color=#FF0000>123<//color>asdf<color=#00FF00>456<//color>",

But what I want is "<color=#FF0000>123<//color>" and "<color=#00FF00>456<//color>",

How can I get the matches I want?


Solution

  • Change .+ to .-.

    + find longest sequence. - find shortest sequence.