简体   繁体   中英

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.

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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