现在的位置: 首页 > 综合 > 正文

Lua正则表达式语言元素

2014年01月07日 ⁄ 综合 ⁄ 共 1195字 ⁄ 字号 评论关闭
  • 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.

抱歉!评论已关闭.