lpeg使用
简介
lua的正则表达式库
规则
lpeg可以创造和组合规则
操作符 | 描述 |
---|---|
lpeg.P(string) | Matches string literally |
lpeg.P(n) | Matches exactly n characters |
lpeg.S(string) | Matches any character in string (Set) |
lpeg.R(“xy”) | Matches any character between x and y (Range) |
patt^n | Matches at least n repetitions of patt |
patt^-n | Matches at most n repetitions of patt |
patt1 * patt2 | Matches patt1 followed by patt2 |
patt1 + patt2 | Matches patt1 or patt2 (ordered choice) |
patt1 - patt2 | Matches patt1 if patt2 does not match |
-patt | Equivalent to (“” - patt) |
#patt |
Matches patt but consumes no input |
lpeg.B(patt) | Matches patt behind the current position, consuming no input |
LPeg also offers the re module
, which implements patterns following a regular-expression style (e.g., [09]+). (This module is 260 lines of Lua code, and of course it uses LPeg to parse regular expressions and translate them to regular LPeg patterns.)
使用
函数功能
- lpeg.match (pattern, subject [, init])
pattern
subject
init 指定subject的开始位置
ret
返回匹配的字符串后面字符的位置
- lpeg.type (value)
ret = "pattern" if value == pattern else nil
3.lpeg.version ()
ret
string of lpeg
- lpeg.setmaxstack (max)
The default limit is 400
构造pattern
- lpeg.P (value)
返回值根据value输入模式确定
Converts the given value into a proper pattern, according to the following rules:
If the argument is a pattern, it is returned unmodified.
If the argument is a string, it