【翻译】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 转载请注明出处 如果觉得文章对你有所帮助,请通过留言或 ...
随机推荐
- @dynamic、@synthesize
声明property属性后,有2种实现选择: @synthesize 编译器期间,让编译器自动生成getter/setter方法. 当有自定义的存或取方法时,自定义会屏蔽自动生成该方法 @dynami ...
- CRS-2800 CRS-4000
I was installing Clusterware using ASM on VMware shared disks. When I created the independent persi ...
- JSP userBean setProperty直接从request中获取参数
JSP userBean setProperty直接从request中获取参数: form表单Jsp: <%@ page language="java" import=&qu ...
- C# 汉字转拼音 将中文转换成拼音
//建立一个公共类,用于转换汉字 public class ChnToPh { //定义拼音区编码数组 private static int[] getValue = new int[] { -,-, ...
- 冒泡排序 - vb.net
Imports System.ThreadingModule Module1 Sub Main() Dim int_array(9) As Integer Dim r ...
- Hadoop权威指南: 专有数据类型
Writable 和 WritableComparable接口 Writable接口 ** Writable接口的主要目的是,当数据在网络上传输或从硬盘读写时,提供数据的序列化和反序列化机智 ** * ...
- js如何判断一个变量是否是数组?
//方法一 var arr = [1,2,3]; var obj = {'name': 'xiaoming','age': 19}; if(arr.constructor == Array){ ale ...
- Android几行代码实现实时监听微信聊天
实现效果: 实时监听当前聊天页面的最新一条消息,如图: 实现原理: 同样是利用AccessibilityService辅助服务,关于这个服务类还不了解的同学可以先看下我上一篇关于 ...
- .net 网站应对压力的一些方案总结
开年比较空,抽时间写个博文,总结下自己工作里的一些应对网站访问压力的技术方案. 自己项目现在大概一天50W的pv.已从前端到后端的顺序总结下自己用的一些方案. 一. 前端页面: 1.首先减少资源的大小 ...
- 从你的全世界切过(胡说八道支持向量机SVM小故事)
背景 据说很久很久以前, 澳门有一家"胡说八道大赌场", 专门提供各种奇奇怪怪的玩法. 其中有一个赌博叫"从你的全世界切过"(连名字也这么奇怪). 玩法是在一张 ...