Pergunta

Suppose I have a string like so:

the quick [color=brown]brown[/color] fox [color=green]jum[/color]p[color=yellow]e[/color]d over the lazy dog

What's a good way of going through it and putting it all in an array where each time the text is within the bounds of a [color] tag it will have that color tag around it? so the word 'jumped' would look like this:

[color=green]j[/color]
[color=green]u[/color]
[color=green]m[/color]
p
[color=yellow]e[/color]
d

Where each line is a new instance in the index.

Currently I'm attempting to do it in what I believe is a really messy way by parsing it heavily...

Foi útil?

Solução

local input_string = 'the quick [color=brown]brown[/color] fox [color=green]jum[/color]p[color=yellow]e[/color]d over the lazy dog'

;('[/color]'..input_string):gsub('(%b[])([^[]*)',
   function(tag, text)
      for c in text:gmatch'.' do
         print(tag == '[/color]' and c or tag..c..'[/color]')
      end
   end
)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top