Search code examples
c#regexedi

Regular Expression for 850 EDI file


I have a web service that parses all types of EDI files and transforms them into XML if need be. In order to know what type of file and customer I am dealing with, I need to do a regular expression in order to get the customer ship to id from the N1 segment in the 850 EDI file. This customer is not using our standard X12 implementation. I need all three values I have in parenthesis in my pattern variable. I cannot seem to get my regular expression to work in order to get the customer ship to id. Can someone tell me where I went wrong with my regular expression? I have included a some sample data from the file. The customer ship to id in this example would be "333333". The bill to name would be "Test123 Information Goes Here" and the code qualifier would be "91".

string input = "ISA`00`          `00`          `01`111111111      `01`222222222      `150629`1243`U`00401`000011282`0`T`!^GS`PO`111111111`222222222`20150629`1243`11282`X`004010^ST`850`0001^BEG`00`NE`4503214505``20150421`^N1`BT`Test123 Information Goes Here`91`333333^";
char segmentDelimiter = input[105];
char elementDelimiter = input[103];
string pattern = String.Format(@"N1{0}BT{0}([A-Za-z0-9]+){0}([A-Za-z0-9]+){0}([A-Za-z0-9]+)\{1}$", elementDelimiter, segmentDelimiter);
Match match = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
string customerShipToID = match.Groups[3].Value;

Solution

  • The problem is that [A-Za-z0-9]+ isn't matching the spaces in "Test123 Information Goes Here". You should be able to simplify your regular expression to the following since the segments are delimited. Also you don't want the anchor at the end as I'm guessing the N1 segment will typically not be the last one in your file. Also I'm not sure you really need the RegexOptions.IgnoreCase since the segment name and qualifier should be upper case. Finally you should use Regex.Escape() on your delimiters to make sure they are escaped if needed instead of assuming the segment delimiter needs to be delimited.

    string input = "ISA`00`          `00`          `01`111111111      `01`222222222      `150629`1243`U`00401`000011282`0`T`!^GS`PO`111111111`222222222`20150629`1243`11282`X`004010^ST`850`0001^BEG`00`NE`4503214505``20150421`^N1`BT`Test123 Information Goes Here`91`333333^";
    char segmentDelimiter = input[105];
    char elementDelimiter = input[103];
    string pattern = string.Format(
        @"N1{0}BT{0}(.*?){0}(.*?){0}(.*?){1}", 
        Regex.Escape(elementDelimiter.ToString()), 
        Regex.Escape(segmentDelimiter.ToString()));
    Match match = Regex.Match(input, pattern);
    string customerShipToID = match.Groups[3].Value;