donglu3087 2017-05-25 06:37
浏览 23

正则表达式设置名字和姓氏中的最大字母数

In a form I ask the user to insert his/her First name and Last name with only a space between them, and no space before and after them.

I found the following code: ^[a-zA-Z]+ [a-zA-Z]+$

But I would wish to put restrictions 15 letters max in first, and 20 letters max in last name.

  • 写回答

1条回答 默认 最新

  • dth34653 2017-05-25 06:38
    关注

    Use a limiting quantifier:

    ^[a-zA-Z]{1,15} [a-zA-Z]{1,20}$
             ^^^^^          ^^^^^^
    

    The {1,15} limiting quantifier tells the engine to match 1 to 15 characters matched with the subpattern that is located immediately to the left of it.

    More from the docs:

    The syntax is {min,max}, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. If the comma is present but max is omitted, the maximum number of matches is infinite. So {0,1} is the same as ?, {0,} is the same as *, and {1,} is the same as +. Omitting both the comma and max tells the engine to repeat the token exactly min times.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计