我想使用 .NET 的 Regex.IsMatch 函数来确定一个字符串是否为 8 个字符长并且与表达式 AK-\d\d\d\d[fF]
匹配。因此,AK-9442F
会匹配但不会匹配 AK-9442F2
或 AK-9442
。我将如何构建表达式?
最佳答案
使用静态 Regex.IsMatch ,你可以这样做:
if (Regex.IsMatch(myTestString, @"^AK-\d{4}[fF]$")) {
// passed
}
应该为您的目的工作。分解后,它是:
^ # must match beginning of the string
AK- # string literal, "AK-"
\d{4} # \d is digit, {4} means must be repeated 4 times
[fF] # either upper or lowercase F
$ # must match end of string
关于.net - n 个字符的字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539160/