用于接受字母数字字符(6-10 个字符)的正则表达式 .NET、C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2190376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:15:02  来源:igfitidea点击:

Regular expression for accepting alphanumeric characters (6-10 chars) .NET, C#

c#regexcharnumbersalphanumeric

提问by Madhu

I am building a user registration form using C# with .NET. I have a requirement to validate user entered password fields. Validation requirement is as below.

我正在使用 C# 和 .NET 构建用户注册表。我需要验证用户输入的密码字段。验证要求如下。

  1. It should be alphanumeric (a-z , A-Z , 0-9)
  2. It should accept 6-10 characters (minimum 6 characters, maximum 10 characters)
  3. With at least 1 alphabet and number (example: stack1over)
  1. 它应该是字母数字 (az , AZ , 0-9)
  2. 它应该接受 6-10 个字符(最少 6 个字符,最多 10 个字符)
  3. 与至少1字母和数字(例如:stack1over

I am using a regular expression as below.

我正在使用如下的正则表达式。

^([a-zA-Z0-9]{6,10})$

It satisfies my first 2 conditions. It fails when I enter only characters or numbers.

它满足我的前两个条件。当我只输入字符或数字时,它会失败。

采纳答案by paxdiablo

Pass it through multiple regexes if you can. It'll be a lot cleaner than those look-ahead monstrosities :-)

如果可以,请通过多个正则表达式传递它。它会比那些超前的怪物干净得多:-)

^[a-zA-Z0-9]{6,10}$
[a-zA-Z]
[0-9]

Though some might consider it clever, it's not necessary to do everythingwith a single regex (or even with any regex, sometimes - just witness the people who want a regex to detect numbers between 75 and 4093).

尽管有些人可能会认为这是聪明的,它没有必要做的一切与一个正则表达式(或者甚至正则表达式的任何,有时-只是见证谁想要一个正则表达式75和4093之间检测数的人)。

Would you rather see some nice clean code like:

你更愿意看到一些漂亮干净的代码,比如:

if not checkRegex(str,"^[0-9]+$")
    return false
val = string_to_int(str);
return (val >= 75) and (val <= 4093)

or something like:

或类似的东西:

return checkRegex(str,"^7[5-9]$|^[89][0-9]$|^[1-9][0-9][0-9]$|^[1-3][0-9][0-9][0-9]$|^40[0-8][0-9]$|^409[0-3]$")

I know which one I'dprefer to maintain :-)

我知道更喜欢维护哪一个:-)

回答by Amarghosh

Use positive lookahead

使用正向前瞻

^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{6,10}$

Look arounds are also called zero-width assertions. They are zero-width just like the start and end of line (^, $). The difference is that lookarounds will actually match characters, but then give up the match and only return the result: match or no match. That is why they are called "assertions". They do not consume characters in the string, but only assert whether a match is possible or not.

环顾四周也称为零宽度断言。它们的宽度为零,就像行 ( ^, $)的开始和结束一样。不同之处在于环视实际上会匹配字符,但随后放弃匹配并仅返回结果:匹配或不匹配。这就是为什么它们被称为“断言”。它们不消耗字符串中的字符,而只断言匹配是否可能。

The syntax for look around:

环顾四周的语法:

  • (?=REGEX)Positive lookahead
  • (?!REGEX)Negative lookahead
  • (?<=REGEX)Positive lookbehind
  • (?<!REGEX)Negative lookbehind
  • (?=REGEX)正向前瞻
  • (?!REGEX)负前瞻
  • (?<=REGEX)正面回顾
  • (?<!REGEX)负面回顾

回答by Brock Hensley