.net - n 个字符的字符串的正则表达式

标签 .net regex

我想使用 .NET 的 Regex.IsMatch 函数来确定一个字符串是否为 8 个字符长并且与表达式 AK-\d\d\d\d[fF] 匹配。因此,AK-9442F 会匹配但不会匹配 AK-9442F2AK-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

GSkinner Test

关于.net - n 个字符的字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539160/

相关文章:

c# - 如何包装项目中的每个函数?

python - 当一行中有 ddd 时,正则表达式模式仅获取 3 位数值

python - 使用正则表达式删除数字

PHP preg_replace - 在结果中包含模式/完整主题

.net - 服务器端相当于 IE 设置 "Check for newer version of stored page"

.net - 如果我从未在另一个事务中读取 volatile 数据,那么 SqlTransaction 中的 IsolationLevel 有什么用?

c# - 如何指示 EventLog TraceListener 在特定的 Log 中创建

Javascript/JQuery : How do I count the words separated with a comma?

php - 如何通过长文本并将其转换为 MySQL 的 Insert 语句

c# - 无法将类型 'System.ValueType' 隐式转换为 'int?'