简体   繁体   English

正则表达式匹配,但不包括

[英]Regular Expression match but not include

I'm trying to use regular expression to only select the bolded (minus the *'s) based on the below string (carriage returns are in the string). 我正在尝试使用正则表达式仅根据以下字符串(回车符在字符串中)选择粗体(减去*)。 Every string that I want to select it out of begins with ISA. 我要从中选择的每个字符串都以ISA开头。 I've been trying to work with this, (?<=ISA\\\\*)(\\w*) , but whenever I start adding additional characters to exclude in the (?<...) part, I no longer find any matches. 我一直在尝试使用(?<=ISA\\\\*)(\\w*) ,但是每当我开始添加要排除在(?<...)部分中的其他字符时,我都找不到任何字符火柴。

ISA*00* *00* *ZZ*SOME STRING *ZZ*99999999 *130605 * 2239*|*00501*000000001*0*P*> ISA * 00 * * 00 * * ZZ *某些字符串* ZZ * 99999999 * 130605 * 2239 * | * 00501 * 000000001 * 0 * P *>

REF*TJ*12345677* 参考* TJ * 12345677 *

REF*PQ*23432211 编号* PQ * 23432211

LX*1 LX * 1

The following should work: 以下应该工作:

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

See http://regex101.com/r/lU8sC8/1 参见http://regex101.com/r/lU8sC8/1

The way it works: 工作方式:

^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

If it's numbers between the 9th and 10th asterisk, then you can use: 如果数字介于第9号和第10号之间,则可以使用:

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

as shown in this rubular 如本所示

I would not use Regex to parse X12 EDI documents. 我不会使用Regex来解析X12 EDI文档。 A good place to start would be here 一个很好的起点将在这里

Looking at that link, you'll find that your field delimiter is ALWAYS at position 104 of the ISA line, your subdelimiter is always at position 105 of the ISA line, and your record delimiter is always at 106. (If they're not there, you don't have valid X12). 查看该链接,您将发现字段定界符始终位于ISA行的位置104,子定界符始终位于ISA行的位置105,而记录定界符则始终位于106。(如果不是)那里您没有有效的X12)。

Using that, I'd do something like this (if you're looking specifically for field 7 of the ISA record): 使用该代码,我会做类似的事情(如果您正在专门查找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];

This also allows you to sort through your other records in a similar fashion, should you need to do so. 如果需要,这还允许您以类似的方式对其他记录进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM