C# 字母数字单词的正则表达式,长度必须为 6 个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/291774/
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-03 21:55:46  来源:igfitidea点击:

regex for alphanumeric word, must be 6 characters long

c#regexalphanumeric

提问by

What is the regex for a alpha numeric word, at least 6 characters long (but at most 50).

字母数字单词的正则表达式是什么,至少 6 个字符长(但最多 50 个)。

回答by Jeremy Ruten

With PCRE regex you could do this:

使用 PCRE 正则表达式,您可以这样做:

/[a-zA-Z0-9]{6,50}/

It would be very hard to do in regex without the min/max quantifiers so hopefully your language supports them.

如果没有最小/最大量词,在正则表达式中很难做到,所以希望你的语言支持它们。

回答by chroder

/[a-zA-Z0-9]{6,50}/

You can use word boundaries at the beginning/end (\b) if you want to actually match a word within text.

如果要实际匹配文本中的单词,可以在开头/结尾 (\b) 使用单词边界。

/\b[a-zA-Z0-9]{6,50}\b/

回答by Peter Boughton

\b\w{6,50}\b

\wis any 'word' character - depending on regex flavour it might be just [a-z0-9_] or it might include others (e.g. accented chars/etc).

\w是任何“单词”字符 - 根据正则表达式的风格,它可能只是 [a-z0-9_] 或者可能包括其他字符(例如重音字符/等)。

{6,50}means between 6 and 50 (inclusive)

{6,50}表示 6 到 50(含)之间

\bmeans word boundary (ensuring the word does not exceed the 50 at either end).

\b表示单词边界(确保单词两端不超过 50)。


After re-reading, it appears that what you want do is ensure the entire text matches? If so...


重读之后,看来你要做的就是确保整个文本匹配?如果是这样的话...

^\w{6,50}$