我有一个web服务,它解析所有类型的EDI文件,并在需要时将它们转换为XML。为了知道我所处理的文件和客户的类型,我需要执行一个正则表达式,以便从850个EDI文件中的N1段获得客户传递id。这个客户没有使用我们的标准X12实现。我需要我的模式变量中括号中的所有三个值。我似乎不能让我的正则表达式工作,以使客户的船到id。有人能告诉我我的正则表达式哪里出错了吗?我已经从文件中提供了一些样本数据。在本例中,客户将发送到id为"333333“。名称为"Test123信息到这里“,代码限定符为"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;发布于 2015-06-30 16:05:55
问题是[A-Za-z0-9]+没有匹配"Test123信息到这里“中的空格。您应该能够将正则表达式简化为以下内容,因为分段是分隔的。另外,您不希望锚点在末尾,因为我猜N1段通常不会是文件中的最后一个。另外,我不确定您是否真的需要RegexOptions.IgnoreCase,因为段名和限定符应该是大写的。最后,您应该在分隔符上使用Regex.Escape(),以确保在需要时对它们进行转义,而不是假设分段分隔符需要被分隔。
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;https://stackoverflow.com/questions/31142689
复制相似问题