简体   繁体   English

我如何用正则表达式检查字符串,该字符串包含12个字符并包含0-9a-f?

[英]How I can check a string with Regular expressions about the string has 12 characters and contains 0-9a-f?

I want to check a string with Regex whether the string has 12 characters and this contain af and 0-9. 我想用正则表达式检查一个字符串,该字符串是否包含12个字符,并且包含af和0-9。

Here my code: 这是我的代码:

mac = "000475af588c"; //12 Characters
Match match = Regex.Match(mac, @"([A-Fa-f0-9]+)");

if (match.Success)
{
    //todo
}

Use {12} around your character class [] to make it 12 在您的角色类[]周围使用{12}使其变为12

Match match = Regex.Match(mac, @"^([A-Fa-f0-9]{12})$");

Alternately you can use case insensitive option as well: 另外,您也可以使用不区分大小写的选项:

Match match = Regex.Match(mac, @"^([a-f0-9]{12})$", RegexOptions.IgnoreCase);

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

相关问题 如何用正则表达式替换字符串? - How can I replace string with regular expressions? 如何使用正则表达式从输入字符串中提取所有非字母数字字符? - How can I extract all non-alphanumeric characters from an input string using Regular Expressions? 正则表达式(.NET)-如何匹配在字符串末尾包含可变位数的模式? - Regular Expressions (.NET) - How can I match a pattern that contains a variable number of digits at the end of the string? 用于检查字符串是否包含一定范围的字符的正则表达式 - Regular expression to check if a string contains certain value in a range of characters 如何获取正则表达式来检查字符串是否只包含字母字符[az]或[AZ]? - How can I get a regex to check that a string only contains alpha characters [a-z] or [A-Z]? 如何检查字符串是否包含单词的所有字符 - How to check if a string contains all of the characters of a word 如何检查一个字符串是否包含 C# 中的另一个字符串 - How can I check if a string contains another string in C# 如何反转包含空格和特殊字符的字符串 - How can I reverse string which contains spaces and special characters 如何使用正则表达式检查2个字符? - How can I check for 2 characters with a regular expression? 我可以在 C# 中将正则表达式与 String.Replace 一起使用吗? - Can I use regular expressions with String.Replace in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM