Take the 2-minute tour ×

I have the following string:

01-21-27-0000-00-048 and it is easy to split it apart because each section is separated by a -, but sometimes this string is represented as 01-21-27-0000-00048, so splitting it is not as easy because the last 2 parts are combined. How can I handle this? Also, what about the case where it might be something like 01-21-27-0000-00.048

In case anyone is curious, this is a parcel number and it varies from county to county and a county can have 1 format or they can have 100 formats.

share|improve this question
Why not just strip out all dashes and periods, and then select the appropriate substrings? –  LittleBobbyTables Oct 15 '12 at 14:33
In the second case it's "separated" by 0? I ask because the last token is 000048 instead of 00-048. –  Tim Schmelter Oct 15 '12 at 14:33
@TimSchmelter - I think in the second case it's not separated by anything? –  LittleBobbyTables Oct 15 '12 at 14:34
Is the pattern of digits always going to be 2, 2, 2, 4, 2, 3? If so, the first comment suggestion would work. If not, then you need a craftier solution. The second example is going to prove problematic under such a scenario, as well, if the digits are not consistent. –  Anthony Pegram Oct 15 '12 at 14:35
It's difficult to come up with an answer to this question without knowing the range of possible formats or the significance of the various parts of the string. For example, would 00048 always be equivalent to 00-048, or might it also represent 000-48? –  phoog Oct 15 '12 at 14:35
show 4 more comments

3 Answers

up vote 4 down vote accepted

This is a very good case for using regular expressions. You string matches the following regexp:


Match the input against this expression, and harvest the six groups of digits from the match:

var str = new[] {
    "01-21-27-0000-00048", "01-21-27-0000-00.048", "01-21-27-0000-00-048"
foreach (var s in str) {
    var m = Regex.Match(s, @"(\d{2})-(\d{2})-(\d{2})-(\d{4})-(\d{2})[.-]?(\d{3})");
    for (var i = 1 /* one, not zero */ ; i != m.Groups.Count ; i++) {
        Console.Write("{0} ", m.Groups[i]);

If you would like to allow for other characters, say, letters in the segments that are separated by dashes, you could use \w instead of \d to denote a letter, a digit, or an underscore. If you would like to allow an unspecified number of such characters within a known range, say, two to four, you can use {2,4} in the regexp instead of the more specific {2}, which means "exactly two". For example,


lets the first segment contain two to three digits or letters, and also allow for letters in segments two and three.

share|improve this answer
I have thought about this. They may not be numbers all the time though and some parts can be between 2 and 3 chars –  Xaisoft Oct 15 '12 at 14:41
@Xaisoft - that would be useful information to post in your original question so people aren't guessing at your intent. –  LittleBobbyTables Oct 15 '12 at 14:42
@Xaisoft Then you need to come up with a defined set of rules that all of your data will fall into. Until you do that you won't be able to come up with the correct solution. I was about to post the same answer as this but using named groups in the regex as I prefer that: ^(?<first>\d{2})-(?<second>\d{2})-(?<third>\d{2})-(?<fourth>\d{4})-(?<fifth>\d{‌​2})[\.-]?(?<sixth>\d{3})$ –  Tobsey Oct 15 '12 at 14:43
@Xaisoft That's even better: regex can easily handle small variations like that. –  dasblinkenlight Oct 15 '12 at 14:43
@dasblinkenlight - How can I modify it to handle something like: 20G-34-41-01-00003.G-0020.04 –  Xaisoft Oct 15 '12 at 14:50
show 1 more comment

Normalize the string first.

I.e. if you know that the last part is always three characters, then insert a - as the fourth-to-last character, then split the resultant string. Along the same line, convert the dot '.' to a dash '-' and split that string.

share|improve this answer
add comment

Replace all the char which are not digit with emptyString('').

then any of your string become in the format like


now you can use the divide it in (2, 2, 2, 4, 2, 3) parts.

share|improve this answer
add comment

Your Answer


