今天在学习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 and , stands for the value of the n-th captured substring (see below). The sequence % 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 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", )
--> 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)
-- 这里只想取出两个值.
local ret1, ret2 = -, -
local func = function(a, b)
-- lua新手, 不知道此处是否还有其他方式可以取出这里的a, b
ret1, ret2 = a, b
end -- 返回值为被操作后的字符串, 和匹配到的数量
local new_str, matched_count = string.gsub(str, pat, func)
print('test_str_gsub result,', new_str, matched_count) return ret1, ret2
end -- 请注意这里pat里面的括号, 代表你想要导出并传递给func的参数
print('got numbers,', test_str_gsub('20-15', '(%d+)-(%d+)'))
1 > test_str_gsub result,     -
2 > got numbers,

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

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

  Continue learning ...

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

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

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

    这阵子在学习lua,今天看到string操作,书中描述string.gsub(sourceString, pattern, replacementString)返回一个字符串,但是我在实际操作中却发现 ...

  2. lua——string之string.gsub

    translated from the lua document string.gsub用法: 函数原型:string.gsub( s, pattern, rep1[, n] ) 函数功能:返回一个和 ...

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

    lua的string函数导出在string module中.在lua5.1,同时也作为string类型的成员方法,因此,我们既可以写成string.gsub (s,……), 也可以s:gsub(). ...

  4. lua中 string.find(查找获取字符串) string.gsub(查找替换字符串) string.sub(截取字符串)

    > aaa='/p/v2/api/winapi/adapter/lgj'> print(string.find(aaa, "^/.+/adapter/(.*)"))1 ...

  5. Lua的string和string库总结

    Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...

  6. Lua 之string库

    标准string库 基础字符串函数 string.len(s) 返回一个字符串的长度,例如 string.rep(s, n) 返回一个新的字符串,该字符串是参数s重复n次得到的结果,例如 )) -- ...

  7. LUA之string的使用

    --string.len(s)          --返回字符串s的长度 --string.rep(s, n)--返回重复n次字符串s的串,你使用string.rep("a", 2 ...

  8. 在lua的string库和正则表达式

    一.前提要了解一下lua 的string几个方法 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作 ...

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

    lua原生解释器对字符串的处理能力是十分有限的,强大的字符串操作能力来自于string库.lua的string函数导出在string module中.在lua5.1,同一时候也作为string类型的成 ...

随机推荐

  1. 关于javascript模块加载技术的一些思考

    前不久有个网友问我在前端使用requireJs和seajs的问题,我当时问他你们公司以前有没有自己编写的javascript库,或者javascript框架,他的回答是什么都没有,他只是听说像requ ...

  2. Windows UDP socket recvfrom返回10054错误的解决办法

    现象: 在Windows 7系统上,A使用UDP socket,调用sendto函数向一个目标地址B发送数据,但是目标地址B没有接收数据,如果A此时立即调用recvfrom试图接收目标地址B发回的数据 ...

  3. 为什么一定要杀掉病毒?---帮一位老师解决MyDocument.exe优盘文件夹图标病毒问题

    最近一位大学老师给我抱怨了一个她遇到的烦恼,一直在纠结,生活都被打乱了,事情大概是这样的: 她的优盘里辛辛苦苦弄好备课文件,放在了优盘里,可是每次上课时,就是找不到文件.有时好多文件都被修改了,非常烦 ...

  4. linux shell program summary

    from:Sep 23 2016 mathematical operation: floating number,bc calculator: we can also use bc in shell ...

  5. ubuntu:activate root

    You must activate the usr of root,when using a pc with a new os of ubuntu. This command: sudo passwd ...

  6. 这里有个坑---entity为null的问题

    这里有个坑,最近加班赶个项目,忽然遇到个这个坑,先记录下来,纯当自己提高.---------每一个遇到的坑总结后都是一比财富. 我们在做项目是会使用ajax返回结果,在返回结果的时候一般选择json数 ...

  7. 我心中的核心组件~MSMQ与Redis队列

    回到目录 这个文章其实是我心中的核心组件的第七回,确实在时间上有些滞后了,但内容并不滞后!本文MSMQ只是个引题,我确实不太想说它,它是微软自己集成的一套消息队列,寄宿在Window服务里,稳定性十在 ...

  8. EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~再续~添加对各只读服务器的心跳检测

    回到目录 上一讲中基本实现了对数据库的读写分离,而在选择只读数据库上只是随机选择,并没有去检测数据库服务器是否有效,如服务器挂了,SQL服务停了,端口被封了等等,而本讲主要对以上功能进行一个实现,并对 ...

  9. PHP数据库操作:从MySQL原生API到PDO

    本文将举详细例子向大家展示PHP是如何使用MySQL原生API.MySQLi面向过程.MySQLi面向对象.PDO操作MySQL数据库的. 为了后面的测试,先建立数据库表test.包含表名user,s ...

  10. [Spring框架]Spring IOC的原理及详解。

    这里感谢 CSDN 的原博客:http://blog.csdn.net/m13666368773/article/details/7802126 看后  受益匪浅,这里再重温一遍Spring IOC ...