I have many different formats of input strings, and I need to split the input string into 3 sections. A few examples below (although there are several possibilities):
1A1 = 1, A, 1
123AA44 = 123, AA, 44
AA44 = empty, AA, 44
44AA = 44, AA, empty
Additional constraints for these sections:
- 1st section is only numeric and is 1-4 characters long
- 2nd section is only alpha and is 1-3 characters long
- 3rd section can be numeric or alpha-numeric and is 1-4 characters long
I have reached the end of my regex knowledge with the code below. It works for every scenario except when one of the 3 sections is left empty in the input. Need some help! Thanks.
Regex regex = new Regex("(?<Section1>[0-9]{1,4})(?<Section2>[a-zA-Z]{1,3})(?<Section3>[0-9a-zA-Z]{1,4})");
Match match = regex.Match(inputString);
string 1 = match.Groups["Section1"].Value;
string 2 = match.Groups["Section2"].Value;
string 3 = match.Groups["Section3"].Value;
Did you try this?