简体   繁体   English

如何检查字符串包含字母或数字 - 正则表达式c#

[英]How to check String Contains Alphabets or Digits-Regular Expression c#

How to check Whether string contains Alphabets or Digits using Regular Expression. 如何使用正则表达式检查字符串是否包含字母或数字。 In a single regular expression How can i achieve this in c#.Currently i am finding alphabets like this 在单个正则表达式中如何在c#中实现此目的。目前我正在找到这样的字母表

 Regex.Matches(folderPath, @"[a-zA-Z]").Count

Whether string contains Alphabets or Digits 字符串是否包含字母或数字

For this case: 对于这种情况:

Regex.Matches(folderPath, @"[a-zA-Z0-9]").Count

should work. 应该管用。

In C#, you do not need regex to check if there are digits or alphabetic characters. 在C#中,您不需要正则表达式来检查是否有数字或字母字符。 Use LINQ with Char.IsLetterOrDigit() : 将LINQ与Char.IsLetterOrDigit()

var hasAlphanum = "!?A1".Any(p => Char.IsLetterOrDigit(p)); // => true
var hasAlphanum1 = "!?1".Any(p => Char.IsLetterOrDigit(p)); // => true
var hasAlphanum2 = "!?".Any(p => Char.IsLetterOrDigit(p));  // => false

The .Any(p => Char.IsLetterOrDigit(p)) part is checking if any character inside the string conforms to the condition inside the brackets. .Any(p => Char.IsLetterOrDigit(p))部分正在检查字符串中的任何字符是否符合括号内的条件。

Do this: 做这个:

string folderPath = "abc123";
Regex.IsMatch(folderPath, @"[a-zA-Z0-9]") // returns true

https://dotnetfiddle.net/faeZND https://dotnetfiddle.net/faeZND

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

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