Say, I have a string that I need to verify the correct format of; e.g. RR1234566-001
(2 letters, 7 digits, dash, 1 or more digits). I use something like:
Regex regex = new Regex(patternString);
if (regex.IsMatch(stringToMatch))
{
return true;
}
else
{
return false;
}
This works to tell me whether the stringToMatch
follows the pattern defined by patternString
. What I need though (and I end up extracting these later) are:
123456
and 001
-- i.e. portions of the stringToMatch
.
Please note that this is NOT a question about how to construct regular expressions. What I am asking is: "Is there a way to match and extract values simultaneously without having to use a split function later?"
return regex.IsMatch(...) // code from the question
orreturn match.Success // code from the accepted solution
returns in if/else are not needed :) – Frank SebastiÃ