发帖
充值
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
7 1

提取字符串中的数字

源码分享 3474 7 2021-9-9 02:52:46

  1. --[[
  2. 提取字符串中的数字
  3. mode = 0 :提取一串连贯的数字,返回数字
  4. mode = 1 :提取一组不连贯的数字,返回数组
  5. ]]--
  6. function XX.ExtractNum(str,mode)
  7.         mode = mode or 0
  8.         if str == nil then
  9.                 return nil
  10.         end
  11.         if mode == 0 then
  12.                 return tonumber(string.match(str,"%d+")) or nil
  13.         elseif mode == 1 then
  14.                 local rets = {}
  15.                 local index = 1
  16.                 for word in string.gmatch(str,"%d+") do
  17.                         table.insert(rets,index,word)
  18.                         index = index + 1
  19.                 end
  20.                 return rets
  21.         else
  22.                 print("XX.ExtractNum:参数mode不正确")
  23.         end
  24.         return nil
  25. end
复制代码


使用道具 举报

2021-9-9 08:45:41
感谢 .这是正则提取吗?
2021-9-9 08:46:21
不连续的就是插入数组?还没看明白
2021-9-9 16:17:01
szmy 发表于 2021-9-9 08:46
不连续的就是插入数组?还没看明白

类似这种, 1/2/3/4. 提取出来就是 {1,2,3,4}
2021-11-25 21:08:47
验证码是这样提取的么
2021-12-2 13:31:09
aa627816231 发表于 2021-11-25 21:08
验证码是这样提取的么

可以用这个来提取短信验证码
2021-12-4 00:28:55
66666666666666666
2021-12-6 16:36:24
来学习了
您需要登录后才可以回帖 立即登录
高级模式
返回
源码分享