【问题标题】:How I can check a string with Regular expressions about the string has 12 characters and contains 0-9a-f?如何使用正则表达式检查字符串是否包含 12 个字符并包含 0-9a-f?
【发布时间】:2014-03-14 08:33:58
【问题描述】:

我想用 Regex 检查一个字符串是否有 12 个字符,并且包含 a-f 和 0-9。

这是我的代码:

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

if (match.Success)
{
    //todo
}

【问题讨论】:

  • 你需要量词,{12}。也可能是主播:^ & $

标签: c# asp.net regex string match


【解决方案1】:

在你的角色类[]周围使用{12}使其成为12

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

您也可以使用不区分大小写的选项:

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

【讨论】:

  • 匹配匹配 = Regex.Match(mac, @"([a-f1-9]{12})");怎么了:(
  • @Tarasov 能告诉我为什么它不起作用,你是如何修复它的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多