cocos lua -字符串匹配规则。

function Split(split_string, splitter)
    -- 以某个分隔符为标准,分割字符串
    -- @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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值