码迷,mamicode.com
首页 > 其他好文 > 详细

lua的string.gsub初使用

时间:2015-08-11 14:00:10      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

  今天在学习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 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 print(got numbers,, test_str_gsub(20-15, (%d+)-(%d+)))
1 > test_str_gsub result,     20-15     1
2 > got numbers,    20    15

    Continue learning ...

 

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

lua的string.gsub初使用

标签:

原文地址:http://www.cnblogs.com/longjianjun/p/4720641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!