简体   繁体   English

C#中接受下划线和空格的Alpha字符串的正则表达式

[英]Regex for alpha number string in c# accepting underscore and white spaces

I already gone through many post on SO . 我已经看过很多关于SO的文章 I didn't find what I needed for my specific scenario. 我没有找到特定情况所需的内容。

I need a regex for alpha numeric string. 我需要一个正则表达式来输入字母数字字符串。 where following conditions should be matched 应符合以下条件

Valid string: 有效字符串:

ameya123 (alphabets and numbers)
ameya (only alphabets)
AMeya12(Capital and normal alphabets and numbers)
Ameya_123 (alphabets and underscore and numbers)
Ameya_   123 (alphabets underscore and white speces)

Invalid string: 无效的字串:

123 (only numbers)
_ (only underscore)
(only space) (only white spaces)
any special charecter other than underscore

what i tried till now: 我到目前为止尝试过的内容:

(?=.*[a-zA-Z])(?=.*[0-9]*[\s]*[_]*)

the above regex is working in Regex online editor however not working in data annotation in c# 上面的正则表达式可以在正则表达式在线编辑器中使用,但是不能在c#中的数据注释中使用

please suggest. 请提出建议。

Based on your requirements and not your attempt, what you are in need of is this: 根据您的要求而不是您的尝试,您需要的是:

^(?!(?:\d+|_+| +)$)[\w ]+$

The negative lookahead looks for undesired matches to fail the whole process. 否定的超前查找将导致整个过程失败的不希望的匹配。 Those are strings containing digits only, underscores only or spaces only. 这些是仅包含数字,仅包含下划线或仅包含空格的字符串。 If they never happen we want to have a match for ^[\\w ]+$ which is nearly the same as ^[a-zA-Z0-9_ ]+$ . 如果它们从未发生,我们希望匹配^[\\w ]+$ ,几乎与^[a-zA-Z0-9_ ]+$

See live demo here 在这里观看现场演示

Explanation : 说明

  • ^ Start of line / string ^行/字符串的开头
  • (?! Start of negative lookahead (?!开始否定前瞻
    • (?: Start of non-capturing group (?:非捕获组的开始
      • \\d+ Match digits \\d+匹配数字
      • | Or 要么
      • _+ Match underscores _+匹配下划线
      • | Or 要么
      • [ ]+ Match spaces [ ]+匹配空间
    • )$ End of non-capturing group immediately followed by end of line / string ( none of previous matches should be found ) )$非捕获组的结尾,紧随其后的是行/字符串的结尾( 不应找到以前的任何匹配项
  • ) End of negative lookahead )否定超前结束
  • [\\w ]+$ Match a character inside the character set up to end of input string [\\w ]+$匹配设置到输入字符串末尾的字符中的一个字符

Note: \\w is a shorthand for [a-zA-Z0-9_] unless u modifier is set. 注意: \\w[a-zA-Z0-9_]的简写,除非设置了u修饰符。

If I understand your requirements correctly, you need to match one or more letters (uppercase or lowercase), and possibly zero or more of digits, whitespace, or underscore. 如果我正确理解您的要求,则需要匹配一个或多个字母(大写或小写),并可能匹配零个或多个数字,空格或下划线。 This implies the following pattern: 这意味着以下模式:

^[A-Za-z0-9\s_]*[A-Za-z][A-Za-z0-9\s_]*$

Demo 演示版

In the demo, I have replaced \\s with \\t \\r , because \\s was matching across all lines. 在演示中,我将\\s替换为\\t \\r ,因为\\s在所有行上都匹配。

Unlike the answers given by @revo and @wiktor, I don't have a fancy looking explanation to the regex. 与@revo和@wiktor给出的答案不同,我对正则表达式没有花哨的解释。 I am beautiful even without my makeup on. 即使不化妆也很美丽。 Honestly, if you don't understand the pattern I gave, you might want to review a good regex tutorial. 老实说,如果您不了解我提供的模式,则可能需要阅读一个好的regex教程。

One problem with your regex is that in annotations, the regex must match and consume the entire string input, while your pattern only contains lookarounds that do not consume any text. 正则表达式的一个问题是,在批注中,正则表达式必须匹配并使用整个字符串输入,而模式仅包含不使用任何文本的环顾四周。

You may use 您可以使用

^(?!\d+$)(?![_\s]+$)[A-Za-z0-9\s_]+$

See the regex demo . 参见regex演示 Note that \\w (when used for a server-side validation, and thus parsed with the .NET regex engine) will also allow any Unicode letters, digits and some more stuff when validating on the server side, so I'd rather stick to [A-Za-z0-9_] to be consistent with both server- and client-side validation. 请注意, \\w (用于服务器端验证时,因此与.NET regex引擎一起解析)还将在服务器端验证时允许任何Unicode字母,数字和更多内容,因此我宁愿坚持[A-Za-z0-9_]与服务器端和客户端验证都一致。

Details 细节

  • ^ - start of string (not necessary here, but good to have when debugging) ^ -字符串的开头(这里没有必要,但是调试时很好用)
  • (?!\\d+$) - a negative lookahead that fails the match if the whole string consists of digits (?!\\d+$) -如果整个字符串由数字组成,则匹配失败,匹配失败
  • (?![_\\s]+$) - a negative lookahead that fails the match if the whole string consists of underscores and/or whitespaces. (?![_\\s]+$) -如果整个字符串包含下划线和/或空格,则匹配失败,匹配失败。 NOTE : if you plan to only disallow ____ or " " like inputs, you need to split this lookahead into (?!_+$) and (?!\\s+$) ) 注意 :如果您打算只禁止____" "类的输入,则需要将此前瞻分为(?!_+$)(?!\\s+$)
  • [A-Za-z0-9\\s_]+ - 1+ ASCII letters, digits, _ and whitespace chars [A-Za-z0-9\\s_]+ -1+ ASCII字母,数字, _和空格字符
  • $ - end of string (not necessary here, but still good to have). $ -字符串结尾(这里没有必要,但是仍然很不错)。

This simple RegEx should do it: 这个简单的RegEx应该做到这一点:

[a-zA-Z]+[0-9_ ]*

One or more Alphabet, followed by zero or more numbers, underscore and Space. 一个或多个字母,后跟零个或多个数字,下划线和空格。

这个应该不错:

[\w\s_]*[a-zA-Z]+[\w\s_]*

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

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