Wednesday, October 2, 2013

Regular expression to allow negative and positive numbers?

Say I have a string that looks like As3H22Br-84Si335Nm+21


The following code will separate the numbers and letters from the string and spit them into 2 separate string arrays.



var numRegx = new Regex(@"\d+");

var numbers = numRegx.Matches(input).Cast<Match>().Select(m => m.Value).ToArray();

var wordRegx = new Regex(@"[a-zA-Z]+");
var words = wordRegx.Matches(input).Cast<Match>().Select(m => m.Value).ToArray();



Right now, it's only accepting just positive numbers. How do I modify the regex(@"\d+") also check for negative numbers?

No comments:

Post a Comment