[英]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. 字符串或空格的开头,5个字母,文字短划线,5个字母,空格或字符串结尾,不区分大小写。
This finds a string that looks like this: pejnd-zxdgn
这会找到一个如下所示的字符串:
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
应匹配:
pejnd-zxdgn
或7ejnd-zxdgn
Should not match 7pejnd-zxdgn
or 7ejn-zxdgn
or p7ejnd-zxdgn 不应该匹配
7pejnd-zxdgn
或7ejn-zxdgn
或p7ejnd-zxdgn
Just add a pattern for digit before the [az]
part. 只需在
[az]
部分之前添加数字模式即可。 And change the quantifier to {4}
: 并将量词更改为
{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. 此外,您可以使用单词边界 - 开头和结尾处的
\\b
,如注释中所述。 So, change your regex to: 所以,将你的正则表达式改为:
/\b[a-z0-9][a-z]{4}-[a-z]{5}\b/i
使用'\\ d'匹配单个数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.