我需要为具有以下要求的字符串定义正则表达式:
Name,Surname我已经尝试了类似^[^1-9\?\*\.\?\$\^\_]{1,20}[,][^1-9\?\*\.\?\$\^\_\-]{1,20}$但你可以找到的东西,它也匹配一个40字符长的字符串.
如何检查整个字符串的最大长度,同时在其中加入1个逗号,显然不在边框处?
谢谢
试试正则表达式:
^(?=[^,]+,[^,]+$)[a-zA-Z,]{1,20}$
Run Code Online (Sandbox Code Playgroud)
说明:
^ : Start anchor
(?=[^,]+,[^,]+$) : Positive lookahead to ensure string has exactly one comma
surrounded by atleast one non-comma character on both sides.
[a-zA-Z,]{1,20} : Ensure entire string is of length max 20 and has only
letters and comma
$ : End anchor
Run Code Online (Sandbox Code Playgroud)
您可以使用前向否定断言来执行此操作:
^(?!.{21})[A-Za-z]+,[A-Za-z]+$
Run Code Online (Sandbox Code Playgroud)
正则表达式现在包含两个部分,实际定义和开头的声明,说从那时起,不会有21个字符.
编辑:
因此,对于上述定义,正则表达式变为
^(?!.{21})[^1-9\?*\.\?\$\^_\,]+,[^1-9\?*\.\?\$\^_\,]+$
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9730 次 |
| 最近记录: |