ruby 字符匹配验证问题

hanluner 2013-04-07 10:56:46
业务中需要输入手机号码,现在我整理了11位手机号码的前三位的区段,现在不知道怎么写匹配验证

比如得到一个手机号 13400004444,如何匹配前三位,求教代码.
...全文
334 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wccxiaoan 2013-04-23
  • 打赏
  • 举报
回复
/(\d\d\d)\d\d\d\d\d\d\d\d/.match("手机号") puts $1 => 前三位 >0<
woodAstone2008 2013-04-17
  • 打赏
  • 举报
回复
当然了 上面那个仅仅是对掐面开头的进行过滤了
woodAstone2008 2013-04-17
  • 打赏
  • 举报
回复
我想说的是 非13 14 15 18 开头的手机号都是非正常的手机号吧 if "手机号".to_s.match(/^1[3|4|5|8]\d{9}$/).blank? return "正确" end
babyding 2013-04-08
  • 打赏
  • 举报
回复
"13400004444"=~/1{1}[2,3,4]{1}[0-9]{1}[0-9]{8}/ this expression will return index when match, else return nil first bit will match 1 and only match one times. the second bit will match 2 or 3 or 4, also only one times. the third bit will match any digit in 0-9, and only match one times. the other bits will match any digit e.g. 13400004444==>0 22400004444==>nil 16400004444==>nil 14400004444==>0 15400004444==>nil

2,763

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ruby/Rails
社区管理员
  • Ruby/Rails社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧