dongxindu8753 2011-11-08 16:40
浏览 60
已采纳

正则表达式匹配用户名的复杂条件

I need to check if string has anything else than what specified below

  • it must begins from letter (either uppercase or lowercase)
  • can have alphabetical characters
  • can have numeric characters
  • can have dashes
  • can have minus sign -
  • can have underscore _
  • can have comas ,
  • can have dots .
  • length from 4 to 35 caracters no more no less

everything else should not be in this string have have

i am stuck on this:

preg_match('/^[\w]{4,35}$/i', $username)
  • 写回答

2条回答 默认 最新

  • douhuocuo9012 2011-11-08 16:54
    关注
    preg_match('/^[\w]{4,35}$/i', $username)
    

    OK lets see what this doesn't work :

    Match the beginning of the string, followed by [a-zA-Z0-9_] at least 4 times with a maximum of 35 times. This is quite different from your requirements.

    Instead what you should use :

    /^[a-zA-Z][-,.\w]{3,34}$/
    

    The case sensitivity i modifier is not needed. Also I don't think this is exactly what you want. Usually you would need to specify a minimum length which you don't. This can match "a" for example (not a good username)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思