I require an algorithm to generate a set of stock item serial numbers, working from a starting serial.
For example, with a starting serial of B10S001
, and generating 3 numbers, the result should be -
B10S001
B10S002
B10S003
However, with a starting serial of B10S998
, and generating 3 numbers, the result should be -
B10S998
B10S999
B10T000
i.e. the number of numerics must remain the same, with the last alpha incrementing if necessary.
Note - I do not know the structure of the first serial number. It could be any length and any combination of alphas and numerics.
The code I'm using at the moment is :
public static string IncrementNumber(string pNumber) {
string newNum = "";
var match = Regex.Match(pNumber, @"(?<=(\D|^))\d+(?=\D*$)");
if (match.Success) {
var number = int.Parse(match.Value) + 1;
newNum = string.Format(
"{0}{1}{2}",
pNumber.Substring(0, match.Index),
number,
pNumber.Substring(match.Index + match.Length));
}
return newNum;
}
This gets me some of the way, but removes the leading zeros, and doesn't increment the alpha. Don't know much about regex (copied above code from a forum post) - any ideas how to achieve this?