简体   繁体   English

C#正则表达式没有空格-字符-下划线

[英]c# regex no white spaces - characters - underscore

I'm trying to figure out how to filter with regex the following examples 我试图找出如何使用正则表达式过滤以下示例

"  test  " -> no
"test" -> yes
"test_test" -> yes
"test123test" -> no

I'v tried to figure it out with the numerous topics on here and the REL reference but I only got absolutely confused and lost! 我试图通过这里的众多主题和REL参考来弄清楚它,但是我只是感到非常困惑和迷茫!

@"^[^a-zA-Z\s]{2-40}$"
@"^[^\d\s]{2-40}$"

both are letting through white spaces everywhere. 两者都让无处不在的空白。

Also I wan to fit in a underscore filter but just _ not every special characters. 另外,我也希望使用下划线过滤器,但不能只使用_而不是每个特殊字符。

help pls and if you could reference me some documentation too it would be grate! 帮助请,如果您也可以参考我一些文档,将不胜感激!

Thanks 谢谢

If you need to match 2 to 40 symbol string that may contain ASCII letters and _ , use 如果需要匹配2至40个可能包含ASCII字母和_符号字符串,请使用

^[a-zA-Z_]{2,40}$

See the regex demo 正则表达式演示

C# declaration: C#声明:

string pat = @"^[a-zA-Z_]{2,40}$";

Details : 详细资料

  • ^ - start of string anchor ^ -字符串锚点的开始
  • [a-zA-Z_]{2,40} - 2 to 40 (as {2,40} is a limiting quantifier ) chars that are lower- or uppercase ASCII letters or _ symbols (the [...] is a character class construct matching just 1 char) [a-zA-Z_]{2,40} - 2至40(如{2,40}是一个限制性的量词 )是小写或大写字母ASCII字符或_符号(在[...]是一个字符只匹配1个字符的构造)
  • $ - end of string anchor $ -字符串锚点的结尾

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

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