• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

lua的string.gsub初使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  今天在学习lua,熟悉项目代码的过程中,发现string.gsub好高级,所以在此mark下。

  以下是lua5.1的官方文档介绍。

string.gsub (s, pattern, repl [, n])

Returns a copy of s in which all occurrences of the pattern have been replaced by a replacement string specified by repl, which may be a string, a table, or a function. gsub also returns, as its second value, the total number of substitutions made.
If repl is a string, then its value is used for replacement. The character % works as an escape character: any sequence in repl of the form %n, with n between 1 and 9, stands for the value of the n-th captured substring (see below). The sequence %0 stands for the whole match. The sequence %% stands for a single %.

If repl is a table, then the table is queried for every match, using the first capture as the key; if the pattern specifies no captures, then the whole match is used as the key.

If repl is a function, then this function is called every time a match occurs, with all captured substrings passed as arguments, in order; if the pattern specifies no captures, then the whole match is passed as a sole argument.

If the value returned by the table query or by the function call is a string or a number, then it is used as the replacement string; otherwise, if it is false or nil, then there is no replacement (that is, the original match is kept in the string).

The optional last parameter n limits the maximum number of substitutions to occur. For instance, when n is 1 only the first occurrence of pattern is replaced.

Here are some examples:

     x = string.gsub("hello world", "(%w+)", "%1 %1")
     --> x="hello hello world world"
     
     x = string.gsub("hello world", "%w+", "%0 %0", 1)
     --> x="hello hello world"
     
     x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
     --> x="world hello Lua from"
     
     x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
     --> x="home = /home/roberto, user = roberto"
     
     x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
           return loadstring(s)()
         end)
     --> x="4+5 = 9"
     
     local t = {name="lua", version="5.1"}
     x = string.gsub("$name%-$version.tar.gz", "%$(%w+)", t)
     --> x="lua-5.1.tar.gz"
string.gsub (s, pattern, repl [, n])

使用范例:

   一、repl为function的情况。

    1、将已知格式字符串中的数字提取出来。

 1 test_str_gsub = function(str, pat)
 2     -- 这里只想取出两个值.
 3     local ret1, ret2 = -1, -1    
 4     local func = function(a, b)
 5         -- lua新手, 不知道此处是否还有其他方式可以取出这里的a, b
 6         ret1, ret2 = a, b    
 7     end
 8 
 9     -- 返回值为被操作后的字符串, 和匹配到的数量
10     local new_str, matched_count = string.gsub(str, pat, func)
11     print('test_str_gsub result,', new_str, matched_count)
12 
13     return ret1, ret2
14 end
15 
16 -- 请注意这里pat里面的括号, 代表你想要导出并传递给func的参数
17 print('got numbers,', test_str_gsub('20-15', '(%d+)-(%d+)'))
1 > test_str_gsub result,     20-15     1
2 > got numbers,    20    15

    2、替换掉字符串中匹配到的部分。

 1 -- 将一段格式替换为一个随机数
 2 parse_random_question = function(ques)
 3     local pat = "@R=(%d+)-(%d+)@"    -- 当然,这个pat也可传参
 4     
 5     local rep_func = function(num1, num2)
 6         return math.random(num1, num2)
 7     end
 8 
 9     ques = string.gsub(ques, pat, rep_func)
10     return ques
11 end
12 
13 print(parse_random_question('中间(@R=1-100@)是一个随机数'))
1 > 中间(20)是一个随机数

 

 

  Continue learning ...

 

  如果大大看见错误地方,还请指正,谢谢。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
centos7安装lua语言环境发布时间:2022-07-22
下一篇:
在VS2008命令行编译Lua发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap