【发布时间】:2019-03-11 16:21:55
【问题描述】:
我有一个可以是任何字符的字符串。我喜欢只提取两个感叹号之间的部分或before第一个或after最后一个:
str = "what)ever!when(ver!time!is!mo/ey"
function getStrPart(str,n) -- return a substring
return str:sub(...) or nil
end
getStrPart(str,0) -- return "what)ever" -- string until the first !
getStrPart(str,1) -- return "when(ver" -- between first and second !
getStrPart(str,2) -- return "time"
getStrPart(str,3) -- return "is"
getStrPart(str,4) -- return "mo/ey"
getStrPart(str,5) -- return nil -- for all n less 0 or > 4 (no of the !)
如果字符串不包含!
str = "whatever"
那么函数应该返回 nil
【问题讨论】:
-
谢谢,工作正常。请给它作为答案,请
-
@Herbert:他不会。不幸的是,他认为应该在 cmets 中回答简单的问题,因此 Lua 标签的“未回答”问题列表对于查找实际上未回答的问题完全没有用处。
标签: regex string lua substring lua-patterns