lua的string.gsub初使用


  今天在學習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 ...

 

  如果大大看見錯誤地方,還請指正,謝謝。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM