验证用户名的正则表达式 [英] Regular expression to validate username

查看:42
本文介绍了验证用户名的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个正则表达式来根据这些条件验证用户名:

I'm trying to create a regular expression to validate usernames against these criteria:

  1. 仅包含字母数字字符,下划线.
  2. 下划线和点不能在结尾开始用户名(例如_username/username_/.username/username.).
  3. 下划线和点不能相邻(例如 user_.name).
  4. 下划线或点不能多次连续使用(例如user__name/user..name).
  5. 字符数必须介于 8 到 20 之间.
  1. Only contains alphanumeric characters, underscore and dot.
  2. Underscore and dot can't be at the end or start of a username (e.g _username / username_ / .username / username.).
  3. Underscore and dot can't be next to each other (e.g user_.name).
  4. Underscore or dot can't be used multiple times in a row (e.g user__name / user..name).
  5. Number of characters must be between 8 to 20.

这是我目前所做的;听起来它强制执行所有标准规则但第五条规则.我不知道如何将第 5 条规则添加到此:

This is what I've done so far; it sounds it enforces all criteria rules but the 5th rule. I don't know how to add the 5th rule to this:

 ^[a-zA-Z0-9]+([._]?[a-zA-Z0-9]+)*$

推荐答案

^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$
 └─────┬────┘└───┬──┘└─────┬─────┘└─────┬─────┘ └───┬───┘
       │         │         │            │           no _ or . at the end
       │         │         │            │
       │         │         │            allowed characters
       │         │         │
       │         │         no __ or _. or ._ or .. inside
       │         │
       │         no _ or . at the beginning
       │
       username is 8-20 characters long

<小时>

如果您的浏览器由于缺乏负面的后视支持而引发错误,请使用以下替代模式:


If your browser raises an error due to lack of negative look-behind support, use the following alternative pattern:

^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$

这篇关于验证用户名的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆