在正则表达式中对“.”和“_”应用限制。

huangapple go评论72阅读模式
英文:

Apply limitation on "." and "_" in RegEx

问题

我想写一个用于验证用户名的正则表达式。用户名必须超过6个字符,并以字母开头(大写或小写),但在之后,可以使用字母、数字、点或下划线。重点是可以选择使用点和下划线,但如果决定使用它们,只能使用一次,如果使用两次或更多,则用户名是错误的。例如:

jamesbrianquinn //正确
james.brianquinn //正确
james.brian_quinn //正确
james.brian.quinn //错误
james_brian_quinn //错误
james.brian__quinn //错误


点和下划线的位置不重要。唯一重要的是它们不应该是用户名的第一个字母,也不应该重复超过一次。

我尝试使用字符集中的“?”来编写正则表达式,但对我来说没有起作用:

/^[a-zA-Z][a-zA-Z0-9.?_?]+$/g


那么,正确的正则表达式是什么?
英文:

I want to write a RegEx for username validation. Username must be more than 6 characters and start with a letter(uppercase or lowercase), but in the following, you can use letters, numbers, dot or underscore. The point is that the use of dot and underscore is optional, but if you decide to use them, you have the right to write them only once, if it is used twice or more, then the username is wrong. For example:

jamesbrianquinn		//ture
james.brianquinn	//true
james.brian_quinn	//true
james.brian.quinn	//false
james_brian_quinn	//false
james.brian__quinn	//false

The location of the dot and underscore is not important. The only important thing is that they should not be the first letter of the username or repeated more than once.

I tried to write RegEx by using "?" for dot and underscore in the character set, but it didn't work for me:

/^[a-zA-Z][a-zA-Z0-9.?_?]+$/g

So, what is the correct RegEx???

答案1

得分: 4

可以使用负向预查来强制最多只能有一个点或下划线:

^(?!.*\..*\.)(?!.*_.*_)[A-Za-z][A-Za-z0-9._]{5,}$

这个模式表示匹配:

  • ^ 用户名的开始
    • (?!.*\..*\.) 断言两个点不会出现(最多一个点)
    • (?!.*_.*_) 断言两个下划线不会出现(最多一个下划线)
    • [A-Za-z] 第一个字符是字母
    • [A-Za-z0-9._]{5,} 五个或更多后续有效字符
  • $ 用户名的结束
英文:

You could use negative lookaheads to enforce a maximum of one dot or underscore:

^(?!.*\..*\.)(?!.*_.*_)[A-Za-z][A-Za-z0-9._]{5,}$

This pattern says to match:

  • ^ start of the username
    • (?!.*\..*\.) assert that two dots do not appear (= max one dot)
    • (?!.*_.*_) assert that two underscores do not appear (= max one _)
    • [A-Za-z] first character is letter
    • [A-Za-z0-9._]{5,} five or more following valid characters
  • $ end of the username

答案2

得分: 2

使用单一前瞻、反向引用和不区分大小写标志 /i

^[A-Z](?!.*([._]).*)[\w.]{5,}$

该模式匹配:

  • ^ 字符串的开头
  • [A-Z] 匹配一个大写字母字符 A-Z
  • (?!.*([._]).*\1) 负向前瞻,断言不会有2次出现 ._,在捕获组 ([._]) 内使用字符类,并使用反向引用来匹配与 \1 表示的相同字符
  • [\w.]{5,} 匹配5次或更多次单词字符或 .
  • $ 字符串的结尾

正则表达式演示

const regex = /^[A-Z](?!.*([._]).*)[\w.]{5,}$/i;
[
  "jamesbrianquinn",
  "james.brianquinn",
  "james.brian_quinn",
  "james.brian.quinn",
  "james_brian_quinn",
  "james.brian__quinn"
].forEach(s =>
  console.log(`${s} --> ${regex.test(s)}`)
);

希望这有帮助!

英文:

Using a single lookahead, a backreference and a case insensitive flag /i

^[A-Z](?!.*([._]).*)[\w.]{5,}$

The pattern matches:

  • ^ Start of string
  • [A-Z] Match a single char A-Z
  • (?!.*([._]).*\1) Negative lookahead, assert not 2 times either . or _ using a character class inside a capture group ([._]) , and using a a backreference to match the same character denoted as \1
  • [\w.]{5,} Match 5 or more times a word char or .
  • $ End of string

Regex demo

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const regex = /^[A-Z](?!.*([._]).*)[\w.]{5,}$/i;
[
  &quot;jamesbrianquinn&quot;,
  &quot;james.brianquinn&quot;,
  &quot;james.brian_quinn&quot;,
  &quot;james.brian.quinn&quot;,
  &quot;james_brian_quinn&quot;,
  &quot;james.brian__quinn&quot;
].forEach(s =&gt;
  console.log(`${s} --&gt; ${regex.test(s)}`)
);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月27日 22:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780870.html
  • javascript
  • regex

如何在 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定