Kevin
Kevin

Reputation: 4848

Using RegEx to get numbers from string, but not getting results expected

I have the following string:

string OnClick = "TxtLinkAction(18294,18298)"

I'm using the following line of code to get the numbers from the string:

var numbers = Regex.Split(OnClick, @"\D+");

I was expecting to get two results:

numbers[0] = "18294"
numbers[1] = "18298"

But, I'm getting the following results, instead:

numbers[0] = ""
numbers[1] = "18294"
numbers[2] = "18298"
numbers[3] = ""

So, I'm wondering, why am I getting four results? What am I doing wrong?

Upvotes: 1

Views: 47

Answers (1)

Khaelex
Khaelex

Reputation: 762

I think you want Regex.Matches http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.matches%28v=vs.110%29.aspx instead of Regex.Split.

Edit: You also want to change \D to \d. "\D" matches any non-digit.

Upvotes: 4

Related Questions