【翻译】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 转载请注明出处 如果觉得文章对你有所帮助,请通过留言或 ...
随机推荐
- 浅谈Java分页技术
话不多言.我们要实现java分页技术,我们首先就需要定义四个变量,他们是: int pageSize;//每页显示多少条记录 int pageNow;//希望现实第几页 int pageCount; ...
- origin添加error bar
增加一列,然后set as Y Error,然后全部选中显示就行了.
- 前端MVVM框架:Knockout.JS(一)
前言 在我们平时开发 Web 应用程序的时候,如果项目不算特别大的话,一般都是拿 jQuery 再配合一些前端 UI 框架就在项目上面应用了.如果页面逻辑稍微复杂的话,那个在写前端 JavaScrip ...
- cookie记忆换肤功能实战Demo
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 选项卡(TabHost)的功能与用法
TabHost是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个便签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆法区域.通过这种方式,就可以在一个容器里放置更多组件,例如手机 ...
- 用NetStream的appendBytes播放FLV
public class MiniStream extends Sprite { private var _buffer:ByteArray = new ByteArray(); private va ...
- Java线程:线程状态
线程可以创建6状态: New()新创建.Runnable(可运行).Blocked(被阻塞). Waiting(等待).Timed waiting(计时等待).Terminated(被终止)1 新建线 ...
- Cocos2d-x 详解坐标系统
这篇博文将介绍一下在cocos2dx中的一些坐标系统概念: 一. (1) OpenGL坐标系 Cocos2D-x以OpenGL和OpenGL ES为基础,所以自然支持OpenGL坐标系.该坐标系原点在 ...
- 关于在官网上下载Eclipse遇到的问题!!
首先Eclipse是什么? Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境. 幸运的是,Eclipse 附带了一 ...
- 小谈JavaScript中this的用法
"this"关键字是JavaScript中广泛应用的一种特性,但它经常也是这门语言中最容易混淆和误解的特性.那么"this"的实际意义是什么?它是如何求值的? ...