Reputation: 6446
why does the code below insert an item to the groupCollection even though "aaa" does not contain 12 digits in a row ?
var groupCollection = Regex.Match("aaa", "\\d{12}").Groups
i'm trying to check if a string contains 12 digits in a row like this:
_def_201208141238_aaaa
Upvotes: 1
Views: 80
Reputation: 7341
// 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
}
Upvotes: 0
Reputation: 63065
var match=Regex.Match("_def_201208141238_aaaa", "\\d{12}");
if(match.Success)
{
// string contains 12 digits in a row
}
Upvotes: 3
Reputation: 11832
The Match
method always need to return something. And it returns match object with success
value false
. I guess you want to use Matches
and get the MatchCollection
. In this case you should get 0 matches.
Upvotes: 0