[英]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
在懒狗身上快速[color = brown]棕色[/ color]狐狸[color = green] jum [/ color] p [color = yellow] e [/ color] d
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? 有什么好的方法可以通过它并将它全部放在一个数组中,每次文本都在[color]标签的范围内时,它周围会有颜色标签? so the word 'jumped' would look like this:
所以'jumped'这个词看起来像这样:
[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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.