正则表达式匹配但不包括

时间:2013-10-29 21:58:21

标签: c# regex

我正在尝试使用正则表达式来仅根据下面的字符串选择粗体(减去*)(回车符在字符串中)。我想从中选择它的每个字符串都以ISA开头。我一直在尝试使用(?<=ISA\\*)(\w*),但每当我开始在(?<...)部分添加要排除的其他字符时,我就找不到任何匹配项了。

ISA * 00 * * 00 * * ZZ * SOME STRING * ZZ * 99999999 * 130605 * 2239 * | * 00501 * 000000001 * 0 * P *&gt;

REF * TJ * 12345677 *

REF * PQ * 23432211

LX * 1

3 个答案:

答案 0 :(得分:6)

以下内容应该有效:

^ISA(?:[^*]*[*][^*]*){8}\*(\d+)\*  

请参阅http://regex101.com/r/lU8sC8/1

它的工作方式:

^ISA          — start of string has "ISA"
(?:           — non-capturing group
[^*]*[*][^*]* — zero or more non-asterisks, followed by asterisk, followed by non-asterisk
{8}           — eight of these
\*            — one more asterisk
(\d+)         — capture one or more digits
\*            — followed by another asterisk

答案 1 :(得分:2)

如果它是第9和第10个星号之间的数字,那么您可以使用:

ISA(?:[^*]*\*){9}(\d+)\*

如此rubular

所示

答案 2 :(得分:1)

我不会使用Regex来解析X12 EDI文档。一个好的起点是here

查看该链接,您会发现您的字段分隔符始终位于ISA行的第104位,您的子定界符始终位于ISA行的第105位,并且您的记录分隔符始终为106.(如果它们“不存在,你没有有效的X12。”

使用它,我会做这样的事情(如果你专门研究ISA记录的第7个字段):

var fieldDelimiter = line[103]; //where 'line' = your ISA Line, and remember 0 based index
var fieldSubDelim = line[104];
var recordDelimiter = line[105];

var fields = line.Split(fieldDelimiter);
var yourField = fields[7];

如果需要,您还可以以类似的方式对其他记录进行排序。