2008-05-27

关于正则表达式

关键字: regular expressions 正则 表达式
"An understanding of regex handling greatly benefits the modern programmer. A complete discussion of this topic is far beyond the scope of this book, but if you're interested see the definitive work Mastering Regular Expressions by Jeffrey Friedl."

对正则表达式处理的理解极大帮助了现在的编程人员,对这个专题完整的讨论超出了本书范畴,但是如果您有兴趣,您可以看看最具权威的著作<Mastering Regular Expressions>,该书由Jeffrey Friedl编写。

——The Ruby Way, Hal Fulton

以网站注册为例,如果网站用户名要求只能由6到12个字母、数字、下划线构成,用ruby写一个用户校验函数:

def validate_login_name(name)
  reg=/^[a-zA-Z0-9_]{6,12}$/
  return (reg.match(name))? true : false
end


也可以是/^\w{6,12}$/,注意^表示开头,$表示结尾,意味着被校验字符串必须完全匹配指定的模式。
评论
发表评论

您还没有登录,请登录后发表评论

seemoon
搜索本博客
最近加入圈子
存档
最新评论