使用正则表达式验证用户标识 [英] Validating a user Id using regular expressions
问题描述
我正在创建用户名验证。我正在使用这个正则表达式:
^ [a-zA-Z0-9] [a-zA-Z0-9_\ - ] {0,4} [a- zA-Z0-9] $
它接受允许小写,大写,数字,连字符和下划线的输入。连字符和下划线不得位于字符串的开头或结尾。我不希望用户名包含两个连续的连字符或下划线,例如 Aquib- -qureshi
或 aquib_ _qureshi
(空格)仅为清晰起见而显示。
我可以使用正则表达式执行此操作,还是必须编写一段代码来查找这些连续字符?
如何做到这一点?任何人都可以帮我一个例子吗?
提前谢谢。
,它接受允许小写,大写,数字,连字符和下划线的输入。连字符和下划线不得位于字符串的开头或结尾。我不希望用户名包含两个连续的连字符或下划线,例如Aquib- -qureshi
或aquib_ _qureshi
(空格)仅为清晰起见而显示。
我可以使用正则表达式执行此操作,还是必须编写一段代码来查找这些连续字符?
如何做到这一点?任何人都可以帮我一个例子吗?
提前致谢。
^([a -Za-Z0-9]((__ | - ?!)[A-ZA-Z0-9_\ - ])+ [A-ZA-Z0-9])
< blockquote>
或
^([a-zA-Z0-9](?(?!__ | - )[A-ZA-Z0-9_\ - ]){0,4} [A-ZA-Z0-9])
I am creating a username validation. I am using this regex:
^[a-zA-Z0-9][a-zA-Z0-9_\-]{0,4}[a-zA-Z0-9]$
which takes input that allows lower case, upper case, numbers, hyphens, and underscores. The hyphen and underscore must not be at the start or end of the string. I do not want the username to include two consecutive hyphens or underscores, likeAquib- -qureshi
oraquib_ _qureshi
(spaces shown for clarity only).
Can I do this using regex or do I have to write a piece of code to look for those consecutive characters?
How to do that? Can anyone help me out with an example?
Thanks in advance.解决方案
which takes input that allows lower case, upper case, numbers, hyphens, and underscores. The hyphen and underscore must not be at the start or end of the string. I do not want the username to include two consecutive hyphens or underscores, like Aquib- -qureshi
or aquib_ _qureshi
(spaces shown for clarity only).
Can I do this using regex or do I have to write a piece of code to look for those consecutive characters?
How to do that? Can anyone help me out with an example?
Thanks in advance.
^([a-zA-Z0-9](?(?!__|--)[a-zA-Z0-9_\-])+[a-zA-Z0-9])
or
^([a-zA-Z0-9](?(?!__|--)[a-zA-Z0-9_\-]){0,4}[a-zA-Z0-9])
这篇关于使用正则表达式验证用户标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!