douyu9012 2012-11-29 07:59
浏览 391
已采纳

PHP:正则表达式字母数字和一些特殊字符

I want to use a regex to limit the characters allowed. That is:

a - z /* a to z */
A - Z /* A to Z */
0 - 9 /* 0 to 9 */
_ - /* underscore & dash */
~ ! @ # $% ^ & * () /* allowed special characters */

and this is my regex function:

function validChr($str) {
    return preg_match('/^[A-Za-z0-9_~\-!@#\$%\^&*\(\)]+$/',$str);
}

I've actually tried it and the result as I want, but I still was not sure. Is my regex is correct? Or are there other forms regex? Please help as I am still new about this regex. Thank you.

  • 写回答

2条回答 默认 最新

  • douxianji6181 2012-11-29 08:14
    关注

    It works as it should.

    You should only add \ before * to escape it.

    Check it out here: Regular expression test

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大