lua 截取字符串中指定位置的数字

lua 截取字符串中指定位置的数字

需求:在一个字符串中提取指定的数字信息。
问题:description:gsub("%D+", “”)会把字符串的所有数字都提取出来。
解决方案:通过string.find()把提取指定位置的信息,再提取当前信息的数字。

Demo

local description = "这是一个带多个123456789数字的<color=#E45C6D>160</color>字符串"
local targetNum1 = description:gsub("%D+", "")
print("取出键值中的数字:"..targetNum1)

local targetKey = "(>%d+</color>)"
local _, _, gotKey = string.find(description, targetKey)
print("取出键值:"..gotKey )

local targetNum2 = gotKey:gsub("%D+", "")
print("取出键值中的数字:"..targetNum2)

Output

取出键值中的数字:123456789456160
取出键值:>160</color>
取出键值中的数字:160

猜你喜欢

转载自blog.csdn.net/qq_36829186/article/details/114329699
今日推荐