我需要一些帮助来创建我的模式。我已经完成了基本部分,但还有一个问题。
假设我有一个字符串如下:
John: I can type in :red:colour! :white:Not in the same :red:wo:green:rd :white:though :(
我有这个代码设置来将颜色与实际值分开:
line = "John: I can type in :red:colour! :white:Not in the same :red:wo:green:rd :white:though :("
for token in string.gmatch(line, "%s?[(%S)]+[^.]?") do
for startpos, token2, endpos in string.gmatch(token, "()(%b::)()") do
print(token2)
token = string.gsub(token, token2, "")
end
print(token)
end
将输出:
John:
I
can
type
in
:red:
colour!
:white:
Not
in
the
same
:red:
:green:
word
:white:
though
:(
当我想要它打印出来时:
John:
I
can
type
in
:red:
colour!
:white:
Not
in
the
same
:red:
wo
:green:
rd
:white:
though
:(
如有任何帮助,我们将不胜感激。
最佳答案
以下代码将为您提供 desired output :
for token in line:gmatch( "(%S+)" ) do
if not token:match( "(:%w-:)([^:]+)" ) then
print(token)
else
for col, w in token:gmatch( "(:%w-:)([^:]+)" ) do
print( col )
print( w )
end
end
end
不过,对于如下字符串,它会失败:
in the sa:yellow:me:pink:long-Words!
关于string - Lua模式分离问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19483230/