简体   繁体   English

正则表达式检查某些字符和数字模式

[英]regex checking for certain pattern of characters and numbers

I have users inputting a file in the format of 我让用户以以下格式输入文件

image[1-4 digits][0-1 alphabet] 图片[1-4位数字] [0-1个字母]

for eg : (valid names) 例如:(有效名称)

image1
image1f
image1B
image2201
image2201a

non valid 无效的

image (no number specified)
imagez (no number specified)
image12a1 (no digit permitted after letter)
image44aa (only one letter allowed after number)

how can i check if the right format has been entered ? 如何检查是否输入了正确的格式?

Not sure if you want php or javascript. 不确定是否要使用php或javascript。 Here is the JS version: 这是JS版本:

/^image(\d{1,4})([A-Za-z])?$/

To extract the number: 要提取数字:

var result = regex.exec(tests[i]),
    isValid = result !== null,
    number = isValid && result[1],
    lastChar = isValid && result[2];

Check out the updated example: http://jsfiddle.net/Ug7XD/2/ 请查看更新的示例: http : //jsfiddle.net/Ug7XD/2/

This should help you... 这应该可以帮助您...

/^image[0-9]{1,4}[a-zA-Z]{0,1}$/

Here is explained. 这里说明。 ^ means start of a string, image well the string, [0-9] is used to identify number from 0 to 9, {1,4} means that numbers must be 1 or 4 times, then [a-zA-Z] checks for characters, and {0,1} checks if there is a 1 character after digit or none, and $ means the end of the string you supply in variable. ^表示字符串的开头,将字符串很好地image[0-9]用于标识0到9之间的数字, {1,4}表示数字必须为1或4倍,然后为[a-zA-Z]检查字符, {0,1}检查数字后是否有1个字符或无,并且$表示您在变量中提供的字符串的结尾。 The / is used to escape the regex string. /用于转义正则表达式字符串。

尝试

$pattern = "/^image\d{1,4}[A-Za-z]$/";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM