简体   繁体   中英

regex for one letter and three number

i tried with this RegEX no solution

^([a-zA-Z]{1})([0-9]{3})$

Test Case: Valid

123d

f311

12d3

99A9

如果组合可以按任何顺序排列,您可能需要明确列出替代方案,具体取决于您的正则表达式支持的内容:

^([a-zA-Z]\d{3}|\d[a-zA-Z]\d{2}|\d{2}[a-zA-Z]\d|\d{3}[a-zA-Z])$

Please check below expression as per your requirement

(?=(?:.*\\d){3})(?=(?:.*[a-zA-Z]){1})^[a-zA-Z\\d]*$

Breakdown :

Look for at least 3 digits:

(?=(?:.*\\d){3})

Look for at least 1 letters:

(?=(?:.*[a-zA-Z]){1})

Define what is allowed between the start and end:

^[a-zA-Z\\d]*$

you can check example here .

You can use a lookahead to check there's exactly one letter (case insensitive) and digits:

^(?=\d*[a-z]\d*$).{4}$

or 4 characters:

^(?=.{4}$)\d*[a-z]\d*$

If the lookahead isn't available you can use @cmbuckley pattern, but you can also factorize it. An example in ERE:

^([0-9]([0-9]([0-9][a-z]|[a-z][0-9])|[a-z][0-9]{2})|[a-z][0-9]{3})$

As an aside: never use the quantifier {1} , each token without quantifier occurs one time by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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