drt5813 2015-05-30 19:04
浏览 223
已采纳

允许空格,unicode字母,数字,下划线,短划线和逗号?

I'm pretty new at the subject preg and I'm using this preg_match condition to check if the user has entered whitespace, unicode letters, digits, underscore or dash:

if(preg_match("/[^\040\pL\pN_-]/u", $term)) {

But now I wanted to allow a comma. So I tried this:

if(preg_match("/[^\040\pL\pN,_-]/u", $term)) {

And it actually works and I just wanted to know why. I just want to understand it better. Why does it have to be ,_- and not -_, for example to allow the comma?

I would really appreciate if someone could explain this to me step by step.

  • 写回答

2条回答 默认 最新

  • dsv38843 2015-05-30 19:08
    关注

    This is because - is used for ranges in square brackets([] -> character classes). And as from the manual: indicates character range, example: 0-9 or a-z.

    So as long as you put it at the end you're fine and don't have to escape it. In all other cases you have to escape it with a backslash e.g. \-.

    Means:

    ,_-  //At the end
    _,-  //At the end
    \-,_ //Escape it
    \-_, //Escape it
    ,\-_ //Escape it
    _\-- //Escape it
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗