手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Lua正则表达式语言元素

[日期:2012-02-18] 来源:Linux社区  作者:chencong112 [字体:大 中 小]

•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的详细信息,或者下载地址请点这里

linux
相关资讯       Lua 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款