[英]Regular Expression to match “CP” followed by 3-5 digits
接受“ CP123”的前两个字母CP和其他3或4或5个数字的正则表达式是什么。
CP[0-9]{3,5}
This will match your requirements: 这将满足您的要求:
^CP\d{3,5}$
The ^ matches the beginning of the string, so that it doesn't allow any characters to the left of "CP". ^匹配字符串的开头,因此不允许在“ CP”左侧添加任何字符。
\\d matches a digit, {3,5} makes it match 3-5 digits. \\ d匹配一个数字,{3,5}使其匹配3-5个数字。
The $ matches the end of the string, so that it doesn't allow any characrers after the digits. $匹配字符串的结尾,因此数字后不允许有任何字符。
If you are using the regular expression in a validation control, you can remove the ^ and $, as that is added by the control: 如果在验证控件中使用正则表达式,则可以删除由控件添加的^和$:
CP\d{3,5}
这适用于所有正则表达式引擎:
CP[0-9]{3,5}
As per update in comments: 根据评论中的更新:
CP\d{1,5}
if you want one to five digits following CP. 如果要在CP后面输入1到5位数字。 Otherwise use
否则使用
CP\d+
if you just want CP followed by at least one digit. 如果您只想让CP后跟至少一位数字。
即使您的问题不是很清楚,这也应该起作用:
r'^CP[0-9]{3,5}$'
Regex regxExp = new Regex( "CP[0-9]{3,5}" );
bool result = regxExp.IsMatch( //Expression );
"$CP123[3-5]^"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.