dongwen4630 2014-05-31 17:37
浏览 39
已采纳

用于空白的PHP正则表达式

I am building a OOPHP web application, I am working on a regular expression that ONLY accepts lower/capital case letter along with white spaces. I have been trying to implement the white spaces but for some reason it not working, normally in JAVACC I do something like this:

(["a" - "z"])+ | (["A" - "Z"])+ | " " | "\t" | "
" | "" 

How can I rewrite the top expression in PHP? This is what I have so far (it works, but I don't know how to implement the white spaces in php).

/^[a-zA-Z]*$/
  • 写回答

2条回答 默认 最新

  • dongxing2710 2014-05-31 17:38
    关注

    You can add to your character class to allow for whitespace. You can either add a space character " " if spaces are all you want to match, or use \s which matches whitespace ( , , \t, \f, and " ")

    /^[a-zA-Z\s]*$/
    

    Regular expression:

    ^               # the beginning of the string
    [a-zA-Z\s]*     # any character of: 'a' to 'z', 'A' to 'Z',
                    # whitespace (
    , , \t, \f, and " ") (0 or more times)
    $               # before an optional 
    , and the end of the string
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题