登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

Lua正则表达式语言元素  

2008-11-01 12:33:18|  分类: Lua |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
  • 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.

 

正则的版本太多了.......


2011-6-14

各种不同风格的正则表达式Lua正则表达式语言元素 - 秒大刀 - 秒大刀的城堡: http://www.greenend.org.uk/rjk/2002/06/regexp.html

  评论这张
 
阅读(1635)| 评论(1)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018