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 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 ...

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

时间: 2024-08-30 05:34:27

lua的string.gsub初使用的相关文章

Lua 中string.gsub(sourceString, pattern, replacementString) 返回值有两个

这阵子在学习lua,今天看到string操作,书中描述string.gsub(sourceString, pattern, replacementString)返回一个字符串,但是我在实际操作中却发现,这个函数其实返回的是两部分内容,一部分是替换后的字符串,一部分是替换长度. myString = "my name is lucy, my phone numbre is 010-88993366." print(string.gsub(myString, "%d",

lua——string之string.gsub

translated from the lua document string.gsub用法: 函数原型:string.gsub( s, pattern, rep1[, n] ) 函数功能:返回一个和pattern匹配,并且用rep1替换的副本.rep1可以是string.table和functin. 第二个返回值n是代表匹配的个数. rep1说明: 如果rep1是string类型,那么rep1用于替换匹配的字符串.%代表转义字符,rep1中%n里面, n的范围是1~9,代表第n个捕获的子串(看

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

lua的string函数导出在string module中.在lua5.1,同时也作为string类型的成员方法,因此,我们既可以写成string.gsub (s,……), 也可以s:gsub(). string.gsub (s, pattern, repl [, n]) 有四个参数,给定字符串,匹配模式.替代字符串,第四个参数是可选的,用来限制替换的范围:表示替换次数限制. 作用就是将所有符合匹配模式的地方都替换成替代字符串.并返回替换后的字符串,以及替换次数. 其中,repl可以是strin

Lua的string和string库总结

Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加深记忆. 个人认为string是Lua编程使用数据结构的时候,重要性仅次于table的类型.十分重要! 一.string基础. Lua并没有字符类型,Lua的string类型表示字符序列.所以,长度为1的string就表示了单个字符.Lua的字符类型有这些特征: 1.string字符序列中的字符采用

Lua 之string库

标准string库 基础字符串函数 string.len(s) 返回一个字符串的长度,例如 print(string.len("hello world")) -- 11 string.rep(s, n) 返回一个新的字符串,该字符串是参数s重复n次得到的结果,例如 print(string.rep("go", 3)) -- gogogo string.lower(s) string.upper(s) 字符串大小写转换,例如 print(string.lower(&q

lua的string库与强大的模式匹配

lua原生解释器对字符串的处理能力是十分有限的,强大的字符串操作能力来自于string库.lua的string函数导出在string module中.在lua5.1,同时也作为string类型的成员方法,因此,我们既可以写成string.upper(s), 也可以s:upper(),选择你喜欢的写法. string.len(s)返回s的长度. string.rep(s, n)返回重复s字符串n次的字符串. string.lower(s)返回一份已将大写转成小写的字符串s的拷贝 lower,upp

LUA之string的使用

--string.len(s)          --返回字符串s的长度 --string.rep(s, n)--返回重复n次字符串s的串,你使用string.rep("a", 2^20)可以创建一个1M bytes的字符串(比如,为了测试需要) --string.lower(s)--将s中的大写字母转换成小写(string.upper将小写转换成大写).如果你想不关心大小写对一个数组进行排序的话,你可以这样: --string.upper(s)--将s中的小写字母转换成大写 --st

在lua的string库和正则表达式

一.前提要了解一下lua 的string几个方法 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作字符串,而是返回一个结果 string.len(s):返回字符串的长度. string.lower(s):变小写. string.upper(s):变大写. string.rep(s,n):将s拷贝n份,并连接起来,返回. string.sub(s,i [,j]):取s中从i开始到j为止的自字符串.

Lua正则表达式(string函数)

下面的表列出了Lua支持的所有字符类: .          任意字符 %a         字母 %c         控制字符 %d         数字 %l          小写字母 %p         标点字符 %s         空白符 %u        大写字母 %w        字母和数字 %x        十六进制数字 %z         代表0的字符 在模式匹配中有一些特殊字符 ( ) . % + - * ? [ ^ $ %用作特殊字符的转义字符,因此 '%.'