-- 以某个分隔符为标准,分割字符串
-- @param split_string 需要分割的字符串
-- @param splitter 分隔符
-- @return 用分隔符分隔好的table
local split_result = {}
local search_pos_begin = 1
local split_pos = 0
while true do
local find_pos_begin, find_pos_end = string.find(split_string, splitter[1], search_pos_begin + 1)
split_pos = split_pos + 1
if not find_pos_begin then
break
end
split_result[#split_result + 1] = string.sub(split_string, search_pos_begin, find_pos_begin - 1)
search_pos_begin = find_pos_end + 1
if search_pos_begin <= string.len(split_string) then
split_result[#split_result + 1] = string.sub(split_string, search_pos_begin)
end
return split_result
end
local content = "{<color;FF000FF;\\<<你好>}{<color;FF000FF;哦>}"
local needsplist = {'({.-})'}
local str
str = Split(str or content, needsplist)
local i,j = 0,0
local last_pos =1
local len = string.len(content)
local element_table ={}
for loop_count = 1, 100 do
i, j = string.find(content, "({.-})", j + 1)-- 匹配规则{face;20},{color:FF0000;您好}
if nil == i or nil == j then --普通文本
table.insert(element_table, {0, string.sub(content, last_pos, len)})
break
else --特殊文本
if 1 ~= i and i ~= last_pos then
table.insert(element_table, {0, string.sub(content, last_pos, len - 1)})
end --表示 图标啊,每个字不同的颜色匹配 eg: {<color;FF0000;你好} -->>您好 的颜色 就是 红色
table.insert(element_table, {1, string.sub(content, i, j)})
end
last_pos = j + 1
end
local parms = ''
for k ,v in next ,element_table do
if v[1] == 0 then
print("普通文本出现了")
print("content: ",v[2])
if v[2] == '' then break end
elseif v[1] >= 1 then
print("特殊文本出现了",v[2])
--开始分割
content = string.gsub(v[2],'{','')
--[[]]-- 匹配一对方括号[] string.find("ABC[]D","[[]]") 4 5
content = string.gsub(content,'}','')
content = string.gsub(content,'<','')
content = string.gsub(content,'>','')
content = Split(content,{'(;)'}) --去掉无用的符号,如果文字需要<和> 请加入\\表示
PrintTable(content)
end
end
local s = 'abebd'
print("替换之后",string.gsub(s,'b','e','2'))
PrintTable(content)