简体   繁体   中英

using lua to parse code syntax?

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

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM