I am parsing an excel document and one column includes n number of serial numbers for each row, separated by a white space.
sample serials: 1108656 1108657 1108658 1108659 1108660 1108661 1108662 1108663 1108664 1108665 1108666
how could I use regex to analyze that string and return a List or IEnumerable where each serial number in the sample is an individual element?
serials are between 5 and 8 numbers long.
I am using C# and the .Net Regex.
If the string is simply numbers separated by spaces I’d suggest going with
String.Splitmethod like this :See the documentation of String.Split.
To have the result as an
IEnumerableyou can simply create aList<string>with the result ofString.Splitlike this :Edit:
After reading the comment, the Regex way would indeed kind of validate the input to make sure no other characters are there, which is a good thing. The regex for this would be as simple as this :
The regex expression [0-9]{5,8} means any digit repeated between 5 and 8 times. Of course this Regex is really simple and will simply capture the good things. For example as tring with 1234567 abcd 7654321 will not give any error, it will simply capture the 2 numbers and silently ignore the letters. You could make a much more complex regex to validate even better. This could be a solid starting reference for regex : http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet