lua 字符串处理
匹配模式(pattern)
- . 任何单个字符
- %a 任何字母
- %c 任何控制字符
- %d 任何数字
- %g 任何除空白符外的可打印字符
- %l 所有小写字母
- %p 所有标点符号
- %s 所有空白字符
- %u 所有大写字母
- %w 所有字母及数字
- %x 所有 16 进制数字符号
- %x (这里 x 是任何非字母/数字的字符) 表示字符 x。如 %% 表示百分号%,%. 表示点号.,%/ 表示斜杠/。
[set]表示 set 中所有字符的联合,找到其中任一字符即表示匹配。可以用 - 连接,如[0-9] 表示 0 到 9 这十个数字。[^set]表示 set 的补集。
字符类与特殊意义符号:
- 单个字符类跟一个 '*',将匹配零个或多个该类字符,匹配尽可能长的串。
- 单个字符类跟一个 '-',将匹配零个或多个该类字符,匹配尽可能短的串。
- 单个字符类跟一个 '+',将匹配一个或多个该类字符,匹配尽可能长的串。
- 单个字符类跟一个 '?',将匹配零个或一个该类字符。
- %n,这里 n 可以从 1 到 9,匹配捕获的第 n 个子串。
- %bxy,这里的 x 和 y 是两个明确的字符,匹配以 x 开始 y 结束。如 %b() 匹配包括小括号在内括起来的字符串。
- %f[set],边境模式。匹配位于 set 内某个字符之前的一个空串,且这个位置的前一个字符不属于 set。
string.byte(s[, i[, j]])
- 返回字符 s[i], s[i+1], ..., s[j] 的数字编码
- i 默认为 1,j 默认等于 i
例:
str = "abcdef"
print(str:byte()) --> 97,'a' 的编码值
print(str:byte(2)) --> 98,'b' 的编码值
print(str:byte(2, 4)) -> 98 99 100
string.char(...)
- 接收整数作为字符的编码值为参数,返回对应的字符
例:
print(string.char(97, 98, 99) --> abc
string.dump(function [, strip])
- 以字符串形式表示一个函数,并返回。返回的字符串可用 loadstring 加载。
- strip 为真,表示去掉函数的调试信息(局部变量名,行号等)
例:
local function print_hello()
print("hello world")
end
local f = string.dump(print_hello, true)
print(f)
local a = loadstring(f)
print(type(a)) --> 'function'
a() --> 'hello world'
string.find(s, pattern [, init[, plain]])
- 查找字符串 s 中第一个匹配到 pattern,返回匹配的起始和结束位置。找不到返回 nil
- init 指定从何处开始查找,默认为 1。负值表示从倒数第几个字符开始找,直到字符串结束。
- plain 为真,则关闭模式匹配。
例:
str = "hello, world"
print(string.find(str, "llo")) --> 3 5
print(string.find(str, "world", -5)) --> 8 12
print(string.find(str, "world", -4)) --> nil
string.format(formating, ...)
- 同 c 里的 sprintf,不支持 *, h, L, l, n, p
- 增加 %q,将一个字符串格式化为两个双引号括起。
例:
str = 'a string with "quotes" and\nnew line'
print(string.format("%q", str))
string.gmatch(s, pattern)
- 返回一个迭代器函数,该函数每次被调用时都会以 pattern 为模式对 s 作匹配,并返回捕获到的值。
例:
s = "hello world from Lua"
g = string.gmatch(s, "%a+")
print(type(g)) --> 'function', g 是一个函数
print(g()) --> 'hello',调用一次则进行一次匹配
print(g()) --> 'world',返回第二次匹配的值
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
print(k, v) --> 打印捕获到的值
end
string.gsub(s, pattern, repl[, n])
- 将字符串中所有匹配的 pattern 都替换成 repl,并返回替换后的字符串。第二个返回值返回匹配次数。
- pattern 中没有设定捕获则默认捕获整个 pattern
- 默认全部进行匹配
例:
s = "hello world, hello world, hello world"
print(s:gsub("world", "sam")) --> hello sam, hello sam, hello sam 3
- 如果有 n 参数,则只替换前 n 个匹配。
例:
s = "hello world, hello world, hello world"
print(s:gsub("world", "sam", 1)) --> hello sam, hello world, hello world 1
print(s:gsub("world", "sam", 2)) --> hello sam, hello sam, hello world 2
- 若 repl 是字符串,则字符串直接替换。repl 中的 %d 表示第几个捕获到的子串。%0 表示整个匹配。%%表示单个百分号。
例:
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", "%1 %1", 1)) --> hello hello world, hello world, hello world 1
print(s:gsub("(%w+)%s*(%w+)", "%2 %1", 1)) --> world hello, hello world, hello world 1
- 若 repl 是表,则每次匹配时都会用第一个捕获值作为键值去查找这张表。
例:
x = {}
x.hello = "HELLO"
x.world = "WORLD"
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> HELLO WORLD, HELLO WORLD, HELLO WORLD 6
- 若 repl 是函数,则每次匹配时调用该函数,捕获值作为参数传入该函数。
例:
function x(str)
return "sam"
end
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> sam sam, sam sam, sam sam 6
- 表或函数的结果如果是 false 或 nil 则不操作,如果是字符串或数字,则进行替换。
例:
x = {}
x.hello = "HELLO"
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> HELLO world, HELLO world, HELLO world 6
function x(str)
return nil
end
s = "hello world, hello world, hello world"
print(s:gsub("(%w+)", x)) --> hello world, hello world, hello world 6
string.len(s)
- 返回字符串长度
例:
print(string.len("hello, world")) --> 12
string.lower(s)
- s 中的大写字符转成小写
例:
print(string.lower("HEllo, woRLD")) --> hello, world
string.upper(s)
- s 中的小写字符转成大写
例:
print(string.upper("HEllo, woRLD")) --> HELLO, WORLD
string.match(s, pattern[, init])
- 在字符串中找到第一个匹配 pattern 部分,并返回捕获值。找不到返回 nil。
- init 指定搜索的起始位置。默认为 1,可以为负数。
例:
s = "hello world, hello world, hello world"
print(string.match(s, "hello")) --> hello
print(string.match(s, "wor%a+")) --> world
string.rep(s, n[, sep])
- 用 sep 连接 n 个 s,并返回
- 默认 sep 为空,即没有 分割符
例:
print(string.rep("hello", 2)) --> hellohello
print(string.rep("hello", 2, ", ")) --> hello, hello
string.reverse(s)
- 反转字符串 s
例:
print(string.reverse("hello")) --> olleh
string.sub(s, i[, j])
- 返回 s 的子串,从 i 开始,j 结束。
- i 和 j 可以为负数。
- j 默认为 -1,即到字符串结束。
例:
print(string.sub("hello", 2)) --> ello
print(string.sub("hello", 2, 4)) --> ell
string.pack(fmt, v1, v2, ...)
string.packsize(fmt)
string.unpack(fmt, s[, pos])
lua 字符串处理的更多相关文章
- Lua字符串库(整理)
Lua字符串库小集 1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回 ...
- lua 字符串
lua 字符串 语法 单引号 双引号 "[[字符串]]" 示例程序 local name1 = 'liao1' local name2 = "liao2" lo ...
- cocos2d-x -Lua 字符串
字符串或串(String)是由数字.字母.下划线组成的一串字符. Lua 语言中字符串可以使用以下三种方式来表示: 单引号间的一串字符. 双引号间的一串字符. [[和]]间的一串字符. 以上三种方式的 ...
- lua字符串
本文内容基于版本:Lua 5.3.0 概述 Lua字符串中的合法字符可以是任何的1字节数据,这包括了C语言中表示字符串结束的'\0'字符,也就是说Lua字符串在内部将以带长度的内存块的形式存储,存储的 ...
- Lua学习九----------Lua字符串
© 版权声明:本文为博主原创文章,转载请注明出处 1.Lua字符串 - ''单引号间的一串字符 - ""双引号之间的一串字符 - [[]]之间的一串字符 2.Lua转义字符 3.字 ...
- Lua 学习之基础篇三<Lua 字符串操作>
Lua字符串可以使用以下三种方式表示: 单引号间的一串字符. 双引号间的一串字符. [[和]]间的一串字符. string = [["Lua"]] print("字符串 ...
- Step By Step(Lua字符串库)
Step By Step(Lua字符串库) 1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string ...
- lua 字符串 正则表达式 转义 特殊字符
string.gsub 函数有三个参数:目标串,模式串,替换串.基本作用是用来查找匹配模式的串,并将使用替换串其替换掉: s = string.gsub("Lua is good" ...
- Lua字符串库
1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回字符串s重复n次的结 ...
- Step By Step(Lua字符串库) (转)
1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回字符串s重复n次的结 ...
随机推荐
- [LOJ#114]k 大异或和
[LOJ#114]k 大异或和 试题描述 这是一道模板题. 给由 n 个数组成的一个可重集 S,每次给定一个数 k,求一个集合 T⊆S,使得集合 T 在 S 的所有非空子集的不同的异或和中,其异或和 ...
- SG函数 与 ICG问题
ICG ICG(Impartial Combinatorial Games)游戏是组合游戏(Combinatorial Games)的一类 满足如下性质: ①有两名玩家 ②两名玩家轮流操作,在一个有限 ...
- 通过rabbitmqadmin管理rabbitmq
# 安装rabbitmq $ sudo apt install rabbitmq-server # 下载rabbitmqadmin管理工具 sudo rabbitmq-plugins enable r ...
- 【CF733F】Drivers Dissatisfaction(最小瓶颈生成树,倍增)
题意:给出一个图,每条边有权值和花费c,每次花费c能使的权值-1.给出一个预算,求减完权值后的一个最小生成树. 思路:感谢CC大神 有这样一个结论:最佳方案里必定存在一种,预算全部花费全部分配在一条边 ...
- 免费CSS鼠标样式代码大全
原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] http://5211.91.tc/sb.htm
- eclipse 的SVN安装
打开eclipse -> Help ->Install New Software选项, 点击Add按钮 根据需要,添加自己需要的版本svn控制器的版本,填写name和url,点击ok. ...
- 【Linux】多进程与多线程之间的区别
http://blog.csdn.net/byrsongqq/article/details/6339240 网络编程中设计并发服务器,使用多进程与多线程 ,请问有什么区别? 答案一: 1,进程:子 ...
- hdu3947 给一些已知(需费用)路径去覆盖一些边 //预先加灌法费用流
River Problem 题意:一个有向树(河流),只有一个汇点1,每条边只有一个出度.有些河道有污染指数xi,必需要治理,有m段路径,可以去覆盖这些,每被覆盖一次,xi降低响应值. :即 给出一些 ...
- 关于[WinError 10054] 远程主机强迫关闭了一个现有的连接。
之前一直用python实现qq邮箱自动发送,都弄的好好的,然后今天一打开,就出现如题的错误,百度了许多,说,可能发送邮件次数过多,被当作是攻击,建议换个邮箱,换了也不行, 最后用手机给电脑分享Wifi ...
- Java 网络通信【01】TCP
不积跬步,无以至千里:不积小流,无以成江海.——<荀子劝学> JAVA中设计网络编程模式的主要有TCP和UDP两种. TCP是属于即时通信,点对点连接进行通信. UDP是通过数据包来进行通 ...