lua string函数
lua的string函数:
参数中的index从1开始,负数的意义是从后开始往前数,比如-1代表最后一个字母
对于string类型的值,可以使用OO的方式处理,如string.byte(s.i)可以被写成s:byte(i)
It also sets a metatable for strings where the __index field points to the string table. Therefore, you can use the string functions in object-oriented style. For instance, string.byte(s, i) can be written as s:byte(i).
函数列表
--------------------------------------------------------------------------------
string.byte 将字符串中的字母转换为数字
string.char 将数字转换成字符串
string.sub(s, i [, j]) 返回字符串s的子串,i是开始位置,j是结束位置。i和j可以是负数。
string.sub(s, , j)返回s的前缀;
string.sub(s, -i)返回s的后缀。
string.rep(s, n)复制字符串n次,返回拷贝
string.reverse(s) s中字符的顺序颠倒,返回
string.format(formatstring, ...)
这个函数跟C语言的printf很像,不同之处在于不支持*,l,L,n,p,h这几种转义码,并且额外加了一个转义码为%q。
这个%q据说可以把字符串格式化为可以安全地被lua编译器读取的格式,替换掉其中的引号、转义字符什么的,不太清楚用法。
参考文章:http://www.cnblogs.com/whiteyun/archive////.html
string.len(s) 返回字符串长度
string.lower(s) 大写转小写
string.upper(s) 小写转大写
string.dump 返回一个string,代表了函数的二进制码,返回的这个string可以被loadstring执行。可以实现函数序列化,函数可以传递了,甚至把函数传递到另一个进程都可以的。可以实现在其他作用域访问不同的地方的函数。
模式匹配
--------------------------------------------------------------------------------
string.find (s, pattern [, init [, plain]])
pattern 没有特殊字符的话就是简单搜索,有的话就是模式匹配
init 指定搜索的初始位置
plain 设定为true的话,关闭模式匹配,pattern中的字符不再被视为特殊字符,此时init必须被指定。如果不需要模式匹配功能的话,最好把这个参数设定为true,否则匹配串中包含特殊字符的话可能会达不到预期的效果
一般的使用很简单,当pattern中包含了一些特殊字符的时候,find具有了“模式匹配”的功能,可以根据pattern匹配目标串中符合模式的字符串。
Tips: 当我们想查找目标串中的所有匹配子串的时候,可以使用init参数循环搜索,每一次从前一次匹配的结束位置开始:
-- 查找字符串中所有新行的位置
local str = "hello world\nhaha\nnihaoma"
local t = {}
local i =
while true do
i = string.find(str, "\n", i+)
if i == nil then break end
table.insert(t, i)
end
pattern 详解:
Character Class: 字符类,代表符合某种条件的字符,单个字符类只表示一个字符
x 字符自身 (where x is not one of the magic characters ^$()%.[]*+-?) represents the character x itself.
. 任意字符 (a dot) represents all characters.
%a 字母 represents all letters.
%c 控制字符 represents all control characters.
%d 数字 represents all digits.
%l 小写字母 represents all lowercase letters.
%p 标点字符 represents all punctuation characters.
%s 空白符 represents all space characters.
%u 大写字母 represents all uppercase letters.
%w 字母和数字 represents all alphanumeric characters.
%x 十六进制数字 represents all hexadecimal digits.
%z 代表0的字符 represents the character with representation .
[set] 使用方括号某些字符形成一个自定义的CharacterClass(Lua称之为char-set,就是指传统正则表达式概念中的括号表达式)。可以使用"-"来表示两个字符之间的范围,比如[-]表示一个0到8的字符。可以在[set]中包含别的Character Class,比如[%d%l]或者[%l%d]表示小写字母和数字;可以在[set]中直接添加字符,比如[]表示二进制数。[^set]表示了这个字符集的补集,比如[^%p]表示所有不是标点的字符。
Tips:上面Character Class的大写形式表示小写所代表的集合的补集。例如,'%A'表示非字母的字符。
Lua的字符类依赖于本地环境,所以 '[a-z]' 可能与 '%l' 表示的字符集不同,需酌情使用。在一般情况下,后者包括'ç' 和 'ã',而前者没有。应该尽可能的使用后者来表示字母,除非出于某些特殊考虑,因为后者更简单、方便、更高效。
Pattern Item: 跟在Character Class后面,修饰符,指定匹配Character Class 多次
a single character class, which matches any single character in the class;
a single character class followed by '*', which matches or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
a single character class followed by '-', which also matches or more repetitions of characters in the class. Unlike '*', these repetition items will always match the shortest possible sequence;
a single character class followed by '+', which matches or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
a single character class followed by '?', which matches or occurrence of a character in the class;
比如匹配C语言中的注释:
"/%*.*%*/" 最长匹配
"/%*.-%*/" 最短匹配
比如匹配lua中的标识符:
"[_%a][_%w]*"
magic characters :指 ^$()%.[]*+-? 等特殊字符
% 特殊字符的转义字符,如%%表示百分号,%.表示点,等等
^和$ ^放在pattern开头,表示只匹配目标串的开头部分;$放在pattern的结尾,表示只匹配目标串的结尾部分
A pattern is a sequence of pattern items. A '^' at the beginning of a pattern anchors the match at the beginning of the subject string. A '$' at the end of a pattern anchors the match at the end of the subject string. At other positions, '^' and '$' have no special meaning and represent themselves.
比如去除字符串首位的空格
function trim (s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
Captures:
A pattern can contain sub-patterns enclosed in parentheses; they describe captures. When a match succeeds, the substrings of the subject string that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern "(a*(.)%w(%s*))", the part of the string matching "a*(.)%w(%s*)" is stored as the first capture (and therefore has number ); the character matching "." is captured with number , and the part matching "%s*" has number .
匹配结果中使用()括号包含起来的部分将被“捕获”保存起来,相当于是保存了匹配结果中的一部分,通过这种方式可以对匹配结果中的一部分进行替换取值等操作。捕获的Captures使用数字编号,编号规则应该是从最外层最左边开始。
如果pattern中指定了捕获,则find的返回值中将包含Captures
比如获取字符串中的key,value:
pair = "name = Anna"
_, _, key, value = string.find(pair, "(%a+)%s*=%s*(%a+)")
print(key, value) --> name Anna
比如把搜到的单词两两替换位置:
x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
--> x="world hello Lua from"
向前引用:我们可以在模式中使用向前引用,'%n'(n代表1-9的数字)表示第d个捕获的拷贝。
看个例子,假定你想查找一个字符串中单引号或者双引号引起来的子串,你可能使用模式 '["'].-["']',但是这个模式对处理类似字符串 "it's all right" 会出问题。为了解决这个问题,可以使用向前引用,使用捕获的第一个引号来表示第二个引号:
s = [[then he said: "it's all right"!]]
a, b, c, quotedPart = string.find(s, "(["'])(.-)%1")
print(quotedPart) --> it's all right
print(c) --> "
问题:
怎样匹配指定数量的字符?比如匹配密码长度为8-16位的字母或数字
另外的几个模式匹配函数
--------------------------------------------------------------------------------
string.match(s, pattern, [, init])
返回第一个匹配pattern的Captures,查找不到返回nil。init指定查找的开始位置。如果没有指定Captures,则整个匹配结果被作为Captures返回
这个函数跟find功能很像,只不过match返回的是匹配结果而find返回的是位置
string.gmatch (s, pattern)
string.find只匹配一次,gmatch给出符合匹配的所有字符串
返回一个迭代函数,每次调用这个迭代函数返回从s中根据pattern捕获的匹配串。如果pattern里没有使用括号()进行捕获的话,返回是符合匹配的整个字符串
Returns an iterator function that, each time it is called, returns the next captures from pattern over string s. If pattern specifies no captures, then the whole match is produced in each call.
未指定Captures的例子,获取一个字符串中的所有单词:
s = "hello world from Lua"
for w in string.gmatch(s, "%a+") do
print(w)
end
指定了Captures的例子,获取一个字符串中的所有key-value,并存入表中:
t = {}
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
t[k] = v
end
string.gsub (s, pattern, repl, [, n])
搜索s中符合pattern的所有字串,使用repl替换,返回替换结果的拷贝。n用来指定替换的个数,默认是全部匹配都替换,如果n为2,则只替换前两个。
repl可以是string,function,table:
repl是string: 使用string替换,需要注意的是string中可以有Captures,具体看例子就明白。
repl是table: table作为一个key-value用于查询,捕获到的第一个Captures作为key,key处的值将被替换为table中指定的value。
repl是functions: 每次匹配成功时都会调用这个function,使用其返回值进行替换。每次调用返回值时会按顺序将所有捕获的子串作为参数按顺序传给这个function
如果table或者function返回的值是nil或者false,替换将不会发生
例子:
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"
一篇关于lua模式匹配的文章:http://www.cnblogs.com/whiteyun/archive////.html
lua string函数的更多相关文章
- Lua string库整理
string库提供了字符串处理的通用函数. 例如字符串查找.子串.模式匹配等. 当在 Lua 中对字符串做索引时,第一个字符从 1 开始计算(而不是 C 里的 0 ). 索引可以是负数,它指从字符串末 ...
- Lua基础 函数(一)
转自: http://blog.csdn.net/wzzfeitian/article/details/8653101 在Lua中,函数是对语句和表达式进行抽象的主要方法.既可以用来处理一些特殊的工作 ...
- Lua function 函数
Lua支持面向对象,操作符为冒号‘:’.o:foo(x) <==> o.foo(o, x). Lua程序可以调用C语言或者Lua实现的函数.Lua基础库中的所有函数都是用C实现的.但这些细 ...
- Lua string.gsub (s, pattern, repl [, n])
lua的string函数导出在string module中.在lua5.1,同时也作为string类型的成员方法,因此,我们既可以写成string.gsub (s,……), 也可以s:gsub(). ...
- lua的函数初识
学习到Lua的函数.认为有必要记下来. 參考教程:Programming in Lua 函数能够以表达式或陈述语句出现,例如以下所看到的: print(8*9, 9/8) a = math.sin(3 ...
- Lua的函数的定义、math数学函数、lua字符串的处理、lua支持的字符串类、模式串中的特殊字符_学习笔记04
Lua的函数的定义.math数学函数 定义函数 function [function name] (param1,param2) [function code] --定义一个函数用来求的两个数字的和 ...
- Lua 常用函数 一
lua_getallocf lua_Alloc lua_getallocf (lua_State *L, void **ud); 返回给定状态机的内存分配器函数.如果 ud 不是 NULL ,Lua ...
- lua闭合函数
function count( ... ) return function( ... ) i = i+ return i end end local func = count(...) print(f ...
- Python 常用string函数
Python 常用string函数 字符串中字符大小写的变换 1. str.lower() //小写>>> 'SkatE'.lower()'skate' 2. str.upper ...
随机推荐
- Python脚本控制的WebDriver 常用操作 <二十六> 上传文件
测试用例场景 上传文件的方法是找到上传文件的对象,通常是的对象.然后直接往这个对象send_keys,传入需要上传文件的正确路径.绝对路径和相对路径都可以,但是上传的文件必须存在,否则会报错. Pyt ...
- [转]init.d解析
本文为转载,放在这里以备忘. init.d指的是/etc/rc.d/init.d目录.本文包括3部分内容1. Linux的引导过程2. 运行级别3. /etc/rc.d/ 与/etc/rc.d/ini ...
- HTTP 错误405.0 - Method Not Allowed
在使用IIS发布网页的时候,出现下图这个问题 那么出现这个问题的原因可能有很多种,但是我目前是这么解决的,下面说一下我个人解决这个问题的方案,仅供参考. 首先,以管理员的身份启动cmd 如果电脑是64 ...
- java 格式化日期(DateFormat)
import java.text.DateFormat; import java.util.Date; /** * 格式化时间类 DateFormat.FULL = 0 * DateFormat.DE ...
- 在Web API中使用Swagger-UI开源组件(一个深坑的解决)
介绍: Swagger-Ui是一个非常棒的Web API说明帮助页,具体详情可自行Google和百度. 官网:http://swagger.io/ GitHub地址:https://github ...
- EditorWindow 和MenuItem
using UnityEngine; using System.Collections; using UnityEditor; public class ClipEventEditor : Edito ...
- Eclipse常用功能
功能(Functions):内置运行java程序的插件(JDT:java develop tools)工作集(WorkSet)(Task list)计划任务管理器(Mylyn)系统配置(Prefere ...
- ios的UIImage的两种不同的图片加载方式 tom猫
在ios的UI交互设计时,对图片的处理是难免的:不同的处理方式会对内存有不同的影响: ********************************************************* ...
- oracle 删除表、数据
truncate ddl语句,删除表中数据,速度要比delete快:且所有自增字段重新开始计数:删除数据保留表结构:删除的数据不进入rollback segment,无法恢复.例: trunc ...
- oracle时间处理
一.当前时间 在sql中直接使用sysdate函数,表示当前时间. 那么,利用当前时间,我们可以获取到当前时间前后指定的时间信息. 1.1 加减法 :: ::40select sysdate-1/24 ...