简体   繁体   English

需要帮助为用户名编写正则表达式

[英]Need help writing a regex for usernames

Hey so I'm looking for a regrex pattern that:嘿,所以我正在寻找一种正则表达式模式:

  • Allows for only two or one _ in a row but no more, can end start and have the _ in between只允许连续两个或一个_但不能更多,可以结束 start 并在其间放置_
  • Max 20, min 3 characters最多 20 个,最少 3 个字符
  • Allows for only one period in a row and can't start or end in a period只允许连续一个期间,并且不能在一个期间开始或结束
  • Accepts AZ and 1-9接受 AZ 和 1-9

Some examples of passes and fails are:通过和失败的一些例子是:

.test #fail
_test #pass
_.test #pass
__test #pass
test_ #pass
te_.st #pass
te.st #pass
..test #fail

So far I have ^[\\w](?!.*?\\.{2})(?!.*?_{2})[\\w.]{1,28}$ , which works for everything but detecting the __ between and at the end of the words.到目前为止,我有^[\\w](?!.*?\\.{2})(?!.*?_{2})[\\w.]{1,28}$ ,它适用于所有但检测__之间和__ Any help would be appreciated!任何帮助,将不胜感激! This has been hurting my brain for hours这已经伤害了我的大脑几个小时

Allows for only one period in a row and can't start or end in a period只允许连续一个期间,并且不能在一个期间开始或结束

Negative lookahead for ^\\. ^\\.负前瞻^\\. , \\.$ , and \\.\\. \\.$\\.\\. from the beginning of the string从字符串的开头

Allows for only two or one _ in a row but no more, can end start and have the _ in between只允许连续两个或一个 _ 但不能更多,可以结束 start 并在其间放置 _

Negative lookahead for __ from the beginning of the string从字符串的开头对__负前瞻

Max 20, min 3 characters最多 20 个,最少 3 个字符

After the other logic is implemented, match and consume 3-20 of the allowed characters其他逻辑实现后,匹配消耗3-20个允许的字符

^(?!.*___|\.|.*\.$|.*\.\.)[a-z1-9_.]{3,20}$

https://regex101.com/r/TUjY13/2 https://regex101.com/r/TUjY13/2

  • ^ - From start of string: ^ - 从字符串开始:
  • (?!.*___|\\.|.*\\.$|.*\\.\\.) - Make sure match doesn't: (?!.*___|\\.|.*\\.$|.*\\.\\.) - 确保匹配不会:
    • .*___ - contain triple underscores .*___ - 包含三个下划线
    • \\. - start with a . - 以.
    • .*\\.$ - end with a . .*\\.$ - 以.
    • .*\\.\\. - contain double periods - 包含双句号
  • [a-z1-9_.]{3,20} - Match 3-20 characters in the string body using all allowed characters (case-insensitive) [a-z1-9_.]{3,20} - 使用所有允许的字符匹配字符串正文中的 3-20 个字符(不区分大小写)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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