正则表达式不工作
本文关键字:工作 正则表达式 | 更新日期: 2023-09-27 18:10:07
为什么即使"aaa"在一行中不包含12位数字,下面的代码也要向groupCollection插入一个项?
var groupCollection = Regex.Match("aaa", "''d{12}").Groups
我试图检查一个字符串是否包含一行中的12个数字:
_def_201208141238_aaaa
var match=Regex.Match("_def_201208141238_aaaa", "''d{12}");
if(match.Success)
{
// string contains 12 digits in a row
}
Match
方法总是需要返回一些东西。并返回success
值为false
的匹配对象。我猜你想用Matches
得到MatchCollection
。在这种情况下,你应该得到0个匹配。
// Option 1
// If you are sure there could be only one match then you can check this boolean flag.
var isSuccess = Regex.IsMatch("aaa", "''d{12}");
// Option 2
// There could be multiple matches.
var matchCollection = Regex.Matches("aaa", "''d{12}");
foreach (Match m in matchCollection)
{
// Process your code for each match
}