Q:模式匹配字符串的相关函数?
A:
--[[ string.find(s, pattern [, init [, plain]])
在字符串"s"中查找第一个与匹配模式"pattern"相匹配的子串,
函数返回子串的開始位置和终止位置。假设未找到返回"nil"。
假设在"pattern"中定义了捕获,捕获物也会在之后依次返回。
"init"可指定从字符串"s"的什么位置開始查找,默觉得1。
"plain"指定是否以模式匹配的形式查找子串,假设"plain"为0,
那么将依据"pattern"的字面意思查找子串。默觉得非0值。]]
s = "Hello World! 123 %a+"
print(string.find(s, "l")) --> 3 3
print(string.find(s, "l", 6)) --> 10 10
print(string.find(s, "lll", 6)) --> nil
print(string.find(s, "%a+", 6)) --> 7 11
print(string.find(s, "%a+", 6, 0)) --> 18 20
-- 通过使用第三个參数能够实现查找字符串中全部匹配"pattern"的子串。
local b = 0
local e = 0
while true do
-- 从匹配位置的下一个位置開始继续查找。
b, e = string.find(s, "%a+", e + 1)
if b == nil then break end
io.write(string.format("%s, ", string.sub(s, b, e)))
--> Hello, World, a,
end
print()
--[[ string.gsub(s, pattern, repl [, n])
将字符串"s"中前"n"个与匹配模式"pattern"相匹配的子串替换为"repl",
"n"假设不指定,默认替换全部的匹配。
函数返回被替换后的字符串。以及替换的次数。
1、假设"repl"是一个字符串(当中能够使用捕获物),那么把这个字符串作为替换品。
特例,"%0"代表整个匹配。
2、假设"repl"是一个"table",每次匹配时都会用第一个捕获物作为键去查这张表。
假设"repl"是一个函数,则在每次匹配时都会以全部捕获物作为參数调用这个函数。
3、假设"pattern"中没有设定捕获。则默认捕获整个"pattern"。
4、假设"table"的查询结果或者函数的返回结果是一个字符串或是个数字时,
都将其作为替换品。而结果为"false"或"nil"时不作替换(即保留匹配前的原始串)。]]
print(string.gsub("Lua is cute", "cute", "great"))
--> Lua is great 1
print(string.gsub("all lii", "l", "x"))
--> axx xii 3
print(string.gsub("Lua is great", "perl", "tcl"))
--> Lua is great 0
print(string.gsub("hello world", "(%w+)", "%1 %1"))
--> hello hello world world 2
print(string.gsub("hello world", "%w+", "%0 %0", 1))
--> hello hello world 1
print(string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1"))
--> world hello Lua from 2
print(string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv))
--> home = /home/roberto, user = roberto
收邮件
算法 字符串转换为以为数组
获取下拉框的值
thinphp 模版标签中 除了volist 和 if 其余的标签中的变量不需要$符号
php 时间戳处理 自定义时间戳
jquery 根据json 生成数据 根据生成的数据选择对应的
php qrcode 二维码 扫描后多行在软件中输出问题
数组转换为字符串
浏览器的同源策略
- 最新文章
-
文件系统特性 COW事务
strlen问题
一个通用的Makefile(针对内核模块类pro)
分治法 合并算法设计
file_operations数据结构分析
架构设计:生产者/消费者模式
Btree(B树)介绍
排序算法 分类: 算法导论 20110323 10:34 131人阅读 评论(0) 收藏
String 匹配算法第32章 分类: 算法导论 20110303 10:43 193人阅读 评论(0) 收藏
读书计划 分类: 工作生活经历 20090919 11:06 171人阅读 评论(0) 收藏
- 热门文章
-
算法入门第2章 分类: 算法导论 20110303 11:11 131人阅读 评论(0) 收藏
String 匹配算法(2)第32章 分类: 算法导论 20110303 10:55 163人阅读 评论(0) 收藏
2010年下半年技术规划 分类: 工作生活经历 20100620 19:19 132人阅读 评论(0) 收藏
转:程序员能力矩阵 20100705 13:21 119人阅读 评论(0) 收藏
2010年3月23日 分类: 工作生活经历 20100323 09:07 213人阅读 评论(2) 收藏
两年的感悟和大学四年的总结 分类: 工作生活经历 20090926 18:20 1234人阅读 评论(3) 收藏
开始攻克Linker and loader 分类: Linkers&&Loaders 20091129 19:27 220人阅读 评论(1) 收藏
专刊文章 Web UI框架引领J2EE新开发模式(代码生成器+手工merge半智能开发)
500G JAVA视频网盘分享 (JEECG开源社区)
js 返回时间 字符串