Given a string
var testData = "1234 test string 987 more test";
I want to be able to use a regex to pull out 1234 and 987. As far as I could tell using
var reg = new Regex(@"?<numbers>\d+");
should do what I want but when I say
var match = reg.match(testData);
I would think that
Assert.AreEqual(match.Groups["numbers"].Captures.Count(), 2);
but it’s only 1. What am I doing wrong? Intuition tells me that the
?<group>
means there can only be 0 or 1 of these values. Should I not be using a named group?
*<group>
doesn’t seem to work in the regex builder in visual studio but I did not try it in my tests.
Why didn’t you use the pattern string as below:
and then get the numbers by:
After that, the
matchesvariable contains 2 Match value which represent for 1234 and 987.You also use the assert as:
Hope it will help you!