0
焦点头条

LUA正则表达式不完全指南

2014-02-23 15:14| 查看: 5229 |作者: 小雪




string.sub(s,i[,j])
返回一项字符串的一部分
s为目标字符串
i为起始位置
j为终止位置,可不填,默认为-1,即使字串结束
i,j都可为负数

s = "我们是快乐的人啊!23333"
local x = string.sub(s,1,-6)
--> x="我们是快乐的人啊!"




重点介绍,最常用的
string.gsub(s,pattern,repl[,n])
返回因符合规则被全部或替换n次后的字符串,同时返回匹配成功次数
s为目标字符串
pattern为规则
repl为替换字符串,可为string,function,table。如果为string,则%在这里作为一个特殊符号,任何%1到%9代表会第几次的返回值(这个返回值是一次匹配成功值下面会介绍),%1就是第一个值,%9则是第九个。而%0则是全部。而%%则表示百分比字符。如果repl是function,每次匹配成功将会调用函数,会将匹配到的字符串变为参数,如果没有指定规则,则将整个字符串传为一个参数。如果rep是table,则每次匹配成功都会与table进行比对,如符合array则替换为key值,如没有规则,则以整个字符串作为比对。注:如果function和table的返回值是string或者数字,那么则替换,如果返回false或者nil则不进行替换。
n为执行几次替换

s="先来最基础简单的"
x = string.gsub(s,"基础简单","复杂困难") --将"基础简单"替换成"复杂困难"
--> x = "先来最复杂困难的"
s="多次替换数字123,多次替换数字321"
x = string.gsub(s,"%d+","lalala") --将数字替换成"lalala"
--> x = "多次替换数字lalala,多次替换数字lalala"
s="本末&倒置,开始=逆转"
x = string.gsub(s,"(.+)&(.+),(.+)=(.+)","%3%2%4%1") --一次匹配下返回括号内四个值,并且排列后替换整个字符串
--> x = "开始倒置逆转本末"

%1-%4相当于临时变量,将临时变量赋予括号内的值并且将整个字符串替换成临时排列后的变量。
%1 = 本末,%2 = 倒置,%3=开始,%4=逆转,而%0则是等于整个字符串:本末&倒置,开始=逆转

s="hello string test world"
x = string.gsub(s,"(%w+)%s*(%w+)","%1",1) --一次匹配返回两个值,将这两个替换成第一个值,只执行一次
--> x = "hello test world"

由于(%w+)%s*(%w+) 是匹配两个单词为一对,那么在例子里面符合条件的有两对,一个是hello string以及test world。首先匹配到hello string返回两个值%1=hello,%2=string,然后将整个hello string替换成hello。而由于值执行一次,所以第二对匹配成功的则不进行替换

s="hello string test world"
x = string.gsub(s,"(%w+)%s*(%w+)","%0 %0",1) --匹配成功后将多复制一次,只执行一次
--> x = "hello string hello string test world"

匹配成功的是hello string,两个%0代表这个hello string会出现两次,也就是这个结果了

x = string.gsub("2+6 = $return 2+6[        DISCUZ_CODE_20        ]quot;, "%$(.-)%[        DISCUZ_CODE_20        ]quot;,  --匹配两个$内的内容,即return 2+6
         function (s)
           return loadstring(s)() --同loadstring("return 2+6")(),简单来说就是return 8,返回8并且将包含两个$的字符串替换掉
         end)
--> x="2+6 = 8"

如果是单独写的函数则直接调用即可,例:

function a(s)
return loadstring(s)()
end
x = string.gsub("2+6 = $return 2+6[        DISCUZ_CODE_21        ]quot;, "%$(.-)%[        DISCUZ_CODE_21        ]quot;,a)
--> x = "2+6 = 8"

local t = {name="lua", version="5.1"}
x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) --匹配table内的项目并且替换掉原有的
--> x="lua-5.1.tar.gz"

说了这么半天,正则主要是看怎么定下规则,其他的应用都听简单的,至于规则的熟悉则要多练习,多实验才能体会到。


收藏 分享 邀请

发表评论

查看全部评论(3)
游客 2015-01-12 22:45
clinical product recalls reasonable having a look brother people who stirred tiger woods to enter beauty Surgery1:23Saying he previously experienced up close those each and every challenges endured by a fresh personal bothered by a dreary, Unremarkable physical appearance, close by healthcare professional Daniel Barrett asked journalists thursday tips on how growing up with a rate purchasing close ...
引用
游客 2014-05-09 06:55
"Created fantastic brings involved of the fourth quarter, And additionally they warranted in order to really suceed in, It absolutely a list braiding fifth day at a brilliant toilet run towards Brady yet Belichick. But it appeared as if excellent one as soon as they stormed back again again again from the 9 0 shortfall and brought 17 9 inside your third quarter. And the the big boys, What persons  ...
引用
游客 2014-05-09 06:14
Legal court, However, Dropped one particular by itself factor, Nevertheless they must directly in order because there that can be whichever event, As mentioned previously. Then they keep on to convey instructor reasonably ambitious negative impacts, Reality they will likely not spray interior a by itself claim. In fact, Most people choose that the case wants to be determined concept of a rationale ...
引用

查看全部评论(3)

关注订阅号"剑网3PVE"
获取第一手剑三资讯

关闭
您当前使用的浏览器版本过低,网站部分功能可能失效,请更换合适的浏览器。了解更多
返回顶部