【翻译】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 转载请注明出处 如果觉得文章对你有所帮助,请通过留言或 ...
随机推荐
- js原生设计模式——9外观模式封装2(小型代码库YJ)
<script type="text/javascript"> //小型代码库YJ封装 var YJ = { //根据id获取元素 ...
- ORACLE Postgresql中文排序
当我们order排序不能够实现我们想要的内容时候,尝试一下NLSSORT这个函数吧 他不仅仅按照姓氏排序,名也会排序: nls_param用于指定语言特征,格式为nls_sort = sor ...
- #图# #dijkstra# ----- OpenJudge 726:ROADS
OpenJudge 726:ROADS 总时间限制: 1000ms内存限制: 65536kB 描述 N cities named with numbers 1 ... N are connected ...
- 005.数组、for、foreach
1.方法的传输传递 值参数:传递的是副本 引用参数:自身 保留自定义的方法中对值的改变 形参影响实参ref:对应的形参和实参都用ref修饰 输出参数:实参不用赋值,但是自定义方法内必须对此参数赋值!! ...
- POJ2187(旋转卡壳)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 35459 Accepted: 10978 ...
- MyBatis 源码分析——动态代理
MyBatis框架是如何去执行SQL语句?相信不只是你们,笔者也想要知道是如何进行的.相信有上一章的引导大家都知道SqlSession接口的作用.当然默认情况下还是使用DefaultSqlSessio ...
- 2017年Unity开发环境与插件配置安装(总体介绍)
最近(2017年初)有朋友问,Unity客户端开发如何在机器配置一般的情况下,配置更高效的开发环境,进一步加快开发进度. 推荐如下: Win10(或者Win8)+Unity5.5.1版本(2017年2 ...
- Ubuntu纯字符界面的一些设置
由于Ubuntu的纯字符界面不支持中文显示,所以进行了一些配置,为了更好的显示 1. 把环境语言配置为英文 在用户目录下的".bashrc"文件的结尾处添加以下内容,然后重新登录 ...
- LVS的十种调度算法
LVS十种调度算法 1.静态调度: ①rr(Round Robin):轮询调度,轮叫调度 轮询调度算法的原理是每一次把来自用户的请求轮流分配给内部中的服务器,从1开始,直到N(内部服务器个数),然后重 ...
- Color.js 增强你对颜色的控制
Color.js是一个能加强前端开发中对颜色处理的第三方库. 假设你已经基本了解色彩通道.色彩空间.色相.饱和度.亮度.不透明度等概念.当然了,毕竟前端算是半只脚踏进设计领域了,相信这些概念难不到你. ...