歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Lua正則表達式語言元素

Lua正則表達式語言元素

日期:2017/3/1 10:31:04   编辑:Linux編程

•x (這裡 x 是指其不是這些轉義字符 ^$()%.[]*+-? 之一) --- 其代表了這個字符本身.
•. --- 代表任何字符
•%a --- 代表任何字母. 即[a-zA-Z]
•%c --- 代表任何的控制字符.
•%d ---代表任何的數字字符. 即[0-9]
•%l --- 代表所有的小寫字母. 即[a-z]
•%p --- 代表所有的標點符號字符.
•%s --- 代表所有空格,tab 字符.
•%u --- 代表所有的大寫字母. 即[A-Z]
•%w --- 代表所有的字母數字. 即[a-zA-Z0-9]
•%x --- 代表16進制數字.
•%z --- 代表字符值是 0 的字符. 注意:值為0 的字符是無法正常表達的在表達式中,如果你要使用他,請使用 %z .
•%x (x是任何非字母和數字的字符) --- 代表字符 x. 這是一種標准的方式來代表應用轉義字符. 任何標點符號字符(即使不是轉義字符) 在其前面添加一個 % 可以用來表示其自己 例如 %% 表示 % , %$ 表示 $.
•[set] --- 代表一個字符集合. 如果要表達一個范圍集合,在范圍開始的字符和結尾的字符之間使用 - , 例如要表達 3,4,5,6 這個集合 可以用 [3456] ,也可以用 [3-6] .上面提到的 %x 也可以用到集合中. 例如, [%w_] 表達所有的字母和數字加一個下劃線.
•[^set] --- 表達所有不出現在集合內的.
總的來說,在Lua中的正則表達式與C#中的是相似的,只是轉義字符由\變成了%。

Lua中的正則表達式語言限定符:

•* -- matches 0 or more repetitions of x. Will always match the longest possible chain.
•+ -- matches 1 or more repetitions of x. Will always match the longest possible chain.
•- -- matches 0 or more repetitions of x. Will always match the shortest possible chain.
•? -- matches 0 or 1 occurence of x.
另外一些用法:

•%n -- n must be a number between 1 and 9. Matches the nth captured substring (see below)
•%bxy -- matches a substring starting with x and ending with y. The substring must also have the same number of x and y.
•^ -- When at the beginning of a pattern, it forces the pattern to match the beginning of a string
•$ -- When at the end of a pattern, it forces the pattern to match the end of a string
•When ^ or $ is anywhere else in a pattern, it has no special meaning.

更多關於Lua的詳細信息,或者下載地址請點這裡

Copyright © Linux教程網 All Rights Reserved