【翻译】LPeg编程指南
| Operator | Description |
lpeg.P(string) |
匹配字符串 |
lpeg.P(n) |
匹配n个字符串 |
lpeg.S(string) |
匹配字符串中任意一个字符 (Set) |
lpeg.R("xy") |
匹配x和y之间的任意一个字符(Range) |
patt^n |
匹配至少n个patt |
patt^-n |
匹配最多n个patt |
patt1 * patt2 |
先匹配patt1 然后接着匹配 patt2 |
patt1 + patt2 |
匹配满足patt1 或者满足patt2 (二选一) |
patt1 - patt2 |
匹配满足patt1而且不满足patt2 |
-patt |
和 ("" - patt)一样 |
#patt |
Matches patt but consumes no input |
lpeg.B(patt) |
Matches patt behind the current position, consuming no input |
- 如果参数是一个pattern,则返回参数pattern。
- 如果参数是一个string,则返回匹配这个字符串的pattern。
- 如果参数是一个非负整数 n, 则返回一个匹配正好是n个字符的字符串的pattern。
- 如果参数是一个负整数 -n, 则只有在输入的字符串还剩下不到n个字符才会成。 lpeg.P(-n) 等同于 -lpeg.P(n) (see the unary minus operation).
- 如果参数是一个 boolean, the result is a pattern that always succeeds or always fails (according to the boolean value), without consuming any input.
- 如果参数是一个table, 则被解读为一个grammar (see Grammars)。
- 如果参数是一个function, 则返回一个pattern,等价于一个 match-time capture 用一个空字符串匹配.
lower = lpeg.R("az")
upper = lpeg.R("AZ")
letter = lower + upper
| Operation | What it Produces |
lpeg.C(patt) |
所有pattern捕获的子串 |
lpeg.Carg(n) |
the value of the nth extra argument to lpeg.match (matches the empty string) |
lpeg.Cb(name) |
the values produced by the previous group capture named name (matches the empty string) |
lpeg.Cc(values) |
the given values (matches the empty string) |
lpeg.Cf(patt, func) |
捕获的结果将作为参数依次被func调用 |
lpeg.Cg(patt [, name]) |
把patt所有的返回值作为一个返回值并指定一个名字 |
lpeg.Cp() |
捕获的位置 |
lpeg.Cs(patt) |
创建一个替代捕获 |
lpeg.Ct(patt) |
把patt中所有的返回值按照父子关系放到一个数组里返回 |
patt / string |
string, with some marks replaced by captures of patt |
patt / number |
the n-th value captured by patt, or no value when number is zero. |
patt / table |
table[c], where c is the (first) capture of patt |
patt / function |
the returns of function applied to the captures of patt |
lpeg.Cmt(patt, function) |
the returns of function applied to the captures of patt; the application is done at match time |
-- matches a numeral and captures its numerical value
number = lpeg.R""^ / tonumber -- matches a list of numbers, capturing their values
list = number * ("," * number)^ -- auxiliary function to add two numbers
function add (acc, newvalue) return acc + newvalue end -- folds the list of numbers adding them
sum = lpeg.Cf(list, add) -- example of use
print(sum:match("10,30,43")) --> 83
【翻译】LPeg编程指南的更多相关文章
- Spark编程指南V1.4.0(翻译)
Spark编程指南V1.4.0 · 简单介绍 · 接入Spark · Spark初始化 · 使用Shell · 在集群上部署代码 ...
- iOS ---Extension编程指南
当iOS 8.0和OS X v10.10发布后,一个全新的概念出现在我们眼前,那就是应用扩展.顾名思义,应用扩展允许开发者扩展应用的自定义功能和内容,能够让用户在使用其他app时使用该项功能.你可以开 ...
- KVC/KVO原理详解及编程指南
一.简介 1.KVC简介 2.KVO简介 二.KVC相关技术 1.Key和Key Path 2.点语法和KVC 3.一对多关系(To-Many)中的集合访问器方法 4.键值验证(Key-Value V ...
- Core Animation编程指南
本文是<Core Animation Programming Guide>2013-01-28更新版本的译文.本文略去了原文中关于OS X平台上Core Animation相关内容.因为原 ...
- App Extension编程指南(iOS8/OS X v10.10)中文版
http://www.cocoachina.com/ios/20141023/10027.html 当iOS 8.0和OS X v10.10发布后,一个全新的概念出现在我们眼前,那就是应用扩展.顾名思 ...
- 【转】 KVC/KVO原理详解及编程指南
原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 前言: 1.本文基本不讲KVC/KVO的用法,只结合网上的资料说说对这种技术的 ...
- iOS多线程编程指南(一)关于多线程编程(转)
原文:http://www.dreamingwish.com/article/ios-multi-threaded-programming-a-multi-threaded-programming.h ...
- 高级Bash脚本编程指南(27):文本处理命令(三)
高级Bash脚本编程指南(27):文本处理命令(三) 成于坚持,败于止步 处理文本和文本文件的命令 tr 字符转换过滤器. 必须使用引用或中括号, 这样做才是合理的. 引用可以阻止shell重新解释出 ...
- 转:KVC/KVO原理详解及编程指南
作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 转载请注明出处 如果觉得文章对你有所帮助,请通过留言或 ...
随机推荐
- Extjs换肤+cookie皮肤记忆功能
http://www.myext.cn/kaifa/a_102.html Ext之家 <title>无标题页</title> <link rel=" ...
- endnote 使用方法
选择需要的期刊格式,复制到收藏夹. 下载安装插件. 鼠标放在需要插入引用的地方. 关键词搜索文献,记得在这之前要把需要的文献保存至endnote online.然后insert就行了.初次安装可能会有 ...
- Pomelo的Protobuf
pomelo的protobuf实现,借助了javascript的动态性,使得应用程序可以在运行时解析proto文件,不需要进行proto文件的编译.pomelo的实现中,为了更方便地解析proto文件 ...
- Nancy简单实战之NancyMusicStore(三):完善商品信息与管理
前言 上一篇,我们做了不少准备,并且还把我们NancyFx音乐商城的首页打造好了.这一篇主要是完善我们在首页的商品浏览问题和添加对商品的管理. 下面开始正题: 商品详情 首先是查看单个商品的详情: 先 ...
- 编写JQuery插件-2
继续上一节的代码 (function(){ /* code */ })() 来我们介绍一下吧,首先定义一个匿名函数 fnction(){/* 这里放置代码 */} 然后用括号括起来, (fnction ...
- 关于data-xxx属性大小写不敏感,不识别大写的几点总结
1.所有标签属性,没有大小写区分,都是小写,如:data-userID,在输出后会变成data-userid,前者只能获取到'undefined'. 2.dataset 自动把 - 转换为驼峰.类似的 ...
- vs基础:无法断点调试dll项目 无法命中
调试vs时,经常会出现,你设置了dll项目的一些断点,可f5之后,这些断点无效.时代定制的程序组的童鞋告诉你解决方法:在解决方案上右键“属性”,点击左侧树“配置属性”-->“配置”,右侧项目列表 ...
- css3 3d小demo
css3 3d案例总结 最近入坑 Web 动画,所以把自己的学习过程记录一下分享给大家.就把最近做的比较好的给大家分享下 1.旋转拼图 首先看下效果 代码主要由HTML和CSS3组成,应该说还是比较简 ...
- css3 3D变形 入门(一)
css3 3D.html div.oembedall-githubrepos { border: 1px solid #DDD; list-style-type: none; margin: 0 0 ...
- nginx : server_name localhost 和 chrome : Provisional headers are shown
问题相关问题现象:解决思路解决方案总结 问题相关 nginx : server_name localhost chrome : Provisional headers are shown 问题现象: ...