Lua intercepts the number at the specified position in the string

Lua intercepts the number at the specified position in the string

Requirements: Extract specified digital information in a string.
Question: description:gsub("%D+", "") will extract all the digits of the string.
Solution: Use string.find() to extract the information at the specified location, and then extract the number of the current information.

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

Guess you like

Origin blog.csdn.net/qq_36829186/article/details/114329699