简体   繁体   中英

Regular expression to allow 1 digit at beginning of 5 letter group

I currently have this regular expression:

/(^| )[az]{5}-[az]{5}( |$)/i

start of string or space, 5 letters, literal dash, 5 letters, space or end of string, case insensitive.

This finds a string that looks like this: pejnd-zxdgn

I need to allow the first letter only to be a digit instead of a letter.

How do I write this?

Edit: To clarify

should match: pejnd-zxdgn or 7ejnd-zxdgn

Should not match 7pejnd-zxdgn or 7ejn-zxdgn or p7ejnd-zxdgn

Just add a pattern for digit before the [az] part. And change the quantifier to {4} :

/(^| )[0-9][a-z]{4}-[0-9][a-z]{4}( |$)/i

After your update, you just want the first character to be digit or character. Also, you can use word boundaries - \\b at the beginning and the end, as noted in comments. So, change your regex to:

/\b[a-z0-9][a-z]{4}-[a-z]{5}\b/i

使用'\\ d'匹配单个数字。

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