简体   繁体   English

RegEx用于C#中的模式

[英]RegEx for pattern in c#

I am still learning RegEx so I need some help on this one. 我仍在学习RegEx,因此在此方面需要一些帮助。

Rules are as below: 规则如下:

  • Min/Max Length of string: 1-5 最小/最大字符串长度:1-5
  • String has to be a whole word 字符串必须完整
  • String can contain: AZ and/or az and/or 0-9 or combination of both only (eg _ - . * are not allowed) 字符串可以包含:AZ和/或z和/或0-9或仅两者的组合(例如_-。*不允许)

Examples: 例子:

12345 – allowed
Os342 – allowed
2O3d3 – allowed
3sdfds dsfsdf – not allowed
Sdfdf.sdfdf –now allowed

What about: 关于什么:

string input = "12345";
bool match = Regex.IsMatch(input, "^[a-z0-9]{1,5}$", RegexOptions.IgnoreCase);

怎么样

^[A-Za-z0-9]{1,5}$
^[a-zA-Z0-9]{1,5}$
  • [a-zA-Z0-9] specifies the ranges allowed [a-zA-Z0-9]指定允许的范围
  • ^ specifies start of string ^指定字符串的开头
  • $ specifies end of string $指定字符串的结尾
  • {1,5} indicates minimum and maximum number of characters for the range {1,5}表示该范围的最小和最大字符数

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

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