.net 字母数字的正则表达式,但至少有一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1051989/
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
regex for alphanumeric, but at least one character
提问by mrblah
In my asp.net page, I have an input box that has to have the following validation on it:
在我的 asp.net 页面中,我有一个输入框,必须对其进行以下验证:
Must be alphanumeric, with at least 1 character (i.e. can't be ALL numbers).
必须是字母数字,至少有 1 个字符(即不能是所有数字)。
回答by cletus
^\d*[a-zA-Z][a-zA-Z0-9]*$
Basically this means:
基本上这意味着:
- Zero or more ASCII digits;
- One alphabetic ASCII character;
- Zero or more alphanumeric ASCII characters.
- 零个或多个 ASCII 数字;
- 1 个字母 ASCII 字符;
- 零个或多个字母数字 ASCII 字符。
Try a few tests and you'll see this'll pass any alphanumeric ASCII string where at least one non-numeric ASCII character is required.
尝试一些测试,您会看到这将传递任何需要至少一个非数字 ASCII 字符的字母数字 ASCII 字符串。
The key to this is the \d*at the front. Without it the regex gets much more awkward to do.
关键是\d*在前面。没有它,正则表达式会变得更加尴尬。
回答by Philippe Leybaert
Most answers to this question are correct, but there's an alternative, that (in some cases) offers more flexibility if you want to change the rules later on:
这个问题的大多数答案都是正确的,但有一个替代方案,如果您想稍后更改规则,则(在某些情况下)提供更大的灵活性:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]+)$
This will match any sequence of alphanumerical characters, but only if the first group also matches the whole sequence. It's a little-known trick in regular expressions that allows you to handle some very difficult validation problems.
这将匹配任何字母数字字符序列,但前提是第一组也匹配整个序列。这是正则表达式中一个鲜为人知的技巧,它允许您处理一些非常困难的验证问题。
For example, say you need to add another constraint: the string should be between 6 and 12 characters long. The obvious solutions posted here wouldn't work, but using the look-ahead trick, the regex simply becomes:
例如,假设您需要添加另一个约束:字符串的长度应介于 6 到 12 个字符之间。此处发布的明显解决方案不起作用,但使用前瞻技巧,正则表达式简单地变为:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]{6,12})$
回答by John Kugelman
^[\p{L}\p{N}]*\p{L}[\p{L}\p{N}]*$
Explanation:
解释:
[\p{L}\p{N}]*matches zero or more Unicode letters or numbers\p{L}matches one letter[\p{L}\p{N}]*matches zero or more Unicode letters or numbers^and$anchor the string, ensuring the regex matches the entirestring. You may be able to omit these, depending on which regex matching function you call.
[\p{L}\p{N}]*匹配零个或多个 Unicode 字母或数字\p{L}匹配一个字母[\p{L}\p{N}]*匹配零个或多个 Unicode 字母或数字^并$锚定字符串,确保正则表达式匹配整个字符串。您可以省略这些,具体取决于您调用的正则表达式匹配函数。
Result: you can have any alphanumeric string except there's got to be a letter in there somewhere.
结果:您可以有任何字母数字字符串,除非某处必须有一个字母。
\p{L}is similar to [A-Za-z]except it will include all letters from all alphabets, with or without accents and diacritical marks. It is much more inclusive, using a larger set of Unicode characters. If you don't want that flexibility substitute [A-Za-z]. A similar remark applies to \p{N}which could be replaced by [0-9]if you want to keep it simple. See the MSDN page on character classesfor more information.
\p{L}类似于,[A-Za-z]除了它将包括所有字母表中的所有字母,带有或不带有重音符号和变音符号。它更具包容性,使用更大的 Unicode 字符集。如果您不想要那种灵活性替代品[A-Za-z]。如果您想保持简单\p{N},可以将其替换为类似的评论[0-9]。有关更多信息,请参阅有关字符类的MSDN 页面。
The less fancy non-Unicode version would be
不那么花哨的非 Unicode 版本是
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
回答by Dexter
^[0-9]*[A-Za-z][0-9A-Za-z]*$
is the regex that will do what you're after. The ^ and $ match the start and end of the word to prevent other characters. You could replace the [0-9A-z] block with \w, but i prefer to more verbose form because it's easier to extend with other characters if you want.
是正则表达式,它将做你所追求的。^ 和 $ 匹配单词的开头和结尾以防止其他字符。您可以用 \w 替换 [0-9A-z] 块,但我更喜欢更详细的形式,因为如果需要,使用其他字符扩展更容易。
Add a regular expression validator to your asp.net page as per the tutorial on MSDN: http://msdn.microsoft.com/en-us/library/ms998267.aspx.
按照 MSDN 上的教程向您的 asp.net 页面添加正则表达式验证器:http: //msdn.microsoft.com/en-us/library/ms998267.aspx。
回答by Welbog
^\w*[\p{L}]\w*$
This one's not that hard. The regular expression reads: match a line starting with any number of word characters (letters, numbers, punctuation (which you might not want)), that contains one letter character (that's the [\p{L}]part in the middle), followed by any number of word characters again.
这个没那么难。正则表达式读取:匹配以任意数量的单词字符(字母、数字、标点符号(您可能不想要))开头的一行,其中包含一个字母字符(即[\p{L}]中间部分),后跟任意数量的单词又是字符。
If you want to exclude punctuation, you'll need a heftier expression:
如果要排除标点符号,则需要一个更复杂的表达式:
^[\p{L}\p{N}]*[\p{L}][\p{L}\p{N}]*$
And if you don't care about Unicode you can use a boring expression:
如果你不关心 Unicode,你可以使用一个无聊的表达式:
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
回答by eKek0
^[0-9]*[a-zA-Z][a-zA-Z0-9]*$
Can be
可
- any number ended with a character,
- or an alphanumeric expression started with a character
- or an alphanumeric expression started with a number, followed by a character and ended with an alphanumeric subexpression
- 任何以字符结尾的数字,
- 或以字符开头的字母数字表达式
- 或以数字开头的字母数字表达式,后跟一个字符并以字母数字子表达式结尾

