lpeg使用

lpeg使用

簡介

lua的正則表達式庫

doc

規則

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.)

使用

函數功能
  1. lpeg.match (pattern, subject [, init])

pattern

subject

init 指定subject的開始位置

ret 返回匹配的字符串後面字符的位置

  1. lpeg.type (value)

ret = "pattern" if value == pattern else nil

3.lpeg.version ()

ret string of lpeg

  1. lpeg.setmaxstack (max)

The default limit is 400

構造pattern
  1. 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 is translated to a pattern that matches the string literally.

If the argument is a non-negative number n, the result is a pattern that matches exactly n characters.

If the argument is a negative number -n, the result is a pattern that succeeds only if the input string has less than n characters left: lpeg.P(-n) is equivalent to -lpeg.P(n) (see the unary minus operation).

If the argument is a boolean, the result is a pattern that always succeeds or always fails (according to the boolean value), without consuming any input.

If the argument is a table, it is interpreted as a grammar (see Grammars).

If the argument is a function, returns a pattern equivalent to a match-time capture over the empty string.
  1. lpeg.B(patt)
Returns a pattern that matches only if the input string at the current position is preceded by patt. Pattern patt must match only strings with some fixed length, and it cannot contain captures.

Like the and predicate, this pattern never consumes any input, independently of success or failure
  1. lpeg.R ({range})
Returns a pattern that matches any single character belonging to one of the given ranges. Each range is a string xy of length 2, representing all characters with code between the codes of x and y (both inclusive).

As an example, the pattern lpeg.R("09") matches any digit, and lpeg.R("az", "AZ") matches any ASCII letter.

  1. lpeg.S (string)
Returns a pattern that matches any single character that appears in the given string. (The S stands for Set.)

As an example, the pattern lpeg.S("+-*/") matches any arithmetic operator.

Note that, if s is a character (that is, a string of length 1), then lpeg.P(s) is equivalent to lpeg.S(s) which is equivalent to lpeg.R(s..s). Note also that both lpeg.S("") and lpeg.R() are patterns that always fail.
  1. lpeg.V (v)
This operation creates a non-terminal (a variable) for a grammar. The created non-terminal refers to the rule indexed by v in the enclosing grammar.
  1. lpeg.locale ([table])
Returns a table with patterns for matching some character classes according to the current locale. The table has fields named alnum, alpha, cntrl, digit, graph, lower, print, punct, space, upper, and xdigit, each one containing a correspondent pattern. Each pattern matches any single character that belongs to its class.

If called with an argument table, then it creates those fields inside the given table and returns that table.
  1. #patt

  1. -patt

  2. patt1 + patt2

  3. patt1 - patt2

  4. patt1 * patt2

  5. patt^n

Grammars

Grammars存在的意義:普通的lpeg語法允許我們用增長的模式進行匹配,不允許使用遞歸模式;
如果要使用遞歸模式,我們需要使用grammars

eg:

equalcount = lpeg.P{
  "S";   -- initial rule name
  S = "a" * lpeg.V"B" + "b" * lpeg.V"A" + "",
  A = "a" * lpeg.V"S" + "b" * lpeg.V"A" * lpeg.V"A",
  B = "b" * lpeg.V"S" + "a" * lpeg.V"B" * lpeg.V"B",
} * -1

每條目錄代表着一條規則,

Captures

正常的匹配返回的都是匹配成功的下一個的位置,如果要取得返回內容,需要使用capture

捕獲分組的使用

  • lpeg.C
  • lpeg.Ct
  • lpeg.Cg lpeg.C的聚合,不用Cg直接使用lpeg.C的組合效果是相同的
  • / 也是一種捕獲,不需要使用C,將匹配的子串丟給另外的函數或者table
  • lpeg.Cs 替換捕獲,類似於string.gsub
cal name = lpeg.C(lpeg.alpha^1)
local colon = ':'
local type = lpeg.C(lpeg.digit^-1)
local value = lpeg.C(lpeg.P(1)^1)
local C = lpeg.Cg(name * colon * type * colon * value)

local str = "name:1:value"

print(C:match(str))

result: name 1 value

捕獲的使用
- 設置捕獲模式
- 進行匹配

捕獲組的使用
- 設置捕獲模式
- 設置捕獲組模式
- 進行match匹配

Ct使用

local name = lpeg.Ct(lpeg.C(lpeg.alpha^1))

local str = "name:1:value"

t = name:match(str)
for k, v in pairs(t) do
    print(k, " : ", v)
end

print(name:match(str))

捕獲組的使用,都是基於lpeg.C進行二次封裝,進行進一步捕獲

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章