簡體   English   中英

我如何從文本文件中讀取特定的塊

[英]how can i read specific block from text file

我需要從此文本中讀取數據塊。 該塊以T|DataObject.EShop.Tic.TicVente|開頭T|DataObject.EShop.Tic.TicVente| 行並以T|DataObject.EShop.Tic.TicPaiement|結尾

我只希望前面的字符串之間以D開頭的行。

W|301500120100407213036|

M|SYP||

T|DataObject.EShop.Tic.TicVente|

C|ArtId|ArtRef|PrxInit|QteArt|PrxEntId|TvaId|TvaTaux|RetourId|RetourMagAchat|RetourTicExtIdAchat|RetourDatAchat|PosteNum|TicId|LigNum|PrxAppel|PrxPaye|DatMaj|

D|18250168145|1825016814503131|1690|-1|0934489998|1|0|C|150||20100406000000|1|009700001|1|1690|1690|20100407093455|

D|18250137020|1825013702002161|750|1|1002689999|1|0|||||1|009700001|2|750|750|20100407093455|

D|18260013233|1826001323336111|1990|1|0935689998|1|0|||||1|009700002|1|1990|1990|20100407103918|
T|DataObject.EShop.Tic.TicPaiement|

C|PosteNum|TicId|LigNum|PaieId|Mnt|DevId|MntDev|Info1|Info2|TransId|TransOK|DatMaj|

D|1|009700001|1|01|-940|SYP|-940|||||20100407093455|

D|1|009700002|1|01|4000|SYP|4000|||||20100407103918|
T|DataObject.EShop.Tic.TicVenteAnnulee|

C|PosteNum|Dat|SessId|CliTypId|CliId|UtilId|LotId|ArtId|ArtRef|PrxInit|QteArt|PrxEntId|TvaId|TvaTaux|RetourId|RetourMagAchat|RetourTicExtIdAchat|RetourDatAchat|TicId|LigNum|PrxAppel|PrxPaye|DatMaj|

D|1|20100407105721|0097001|||6|0150010097763|18250040037|1825004003704121|990|1|1002689999|1|0|||||009700004|1|990|990|20100407213033|T|DataObject.EShop.Tic.TicVenteAnnulee|

C|PosteNum|Dat|SessId|CliTypId|CliId|UtilId|LotId|ArtId|ArtRef|PrxInit|QteArt|PrxEntId|TvaId|TvaTaux|RetourId|RetourMagAchat|RetourTicExtIdAchat|RetourDatAchat|TicId|LigNum|PrxAppel|PrxPaye|DatMaj|

D|1|20100407105721|0097001|||6|0150010097763|18250040037|1825004003704121|990|1|1002689999|1|0|||||009700004|1|990|990|20100407213033|

那正則表達式呢?

你說:

從“ T | DataObject.EShop.Tic.TicVente |”開始

因此它將以“ ^ T | DataObject.EShop.Tic.TicVente | $”開頭

你說:

T | DataObject.EShop.Tic.TicPaiement |

因此它將以“ $ T | DataObject.EShop.Tic.TicPaiement | $”結尾

你還需要什么? 每行以D開頭? 好...試試看

Regex rgx = new Regex("^T|DataObject.EShop.Tic.TicVente|$(D[.]*)$T|DataObject.EShop.Tic.TicPaiement|$", RegexOptions.MultiLine);

或者您可以輕松解析

設置一個標志,指示您正在搜索起始字符串
讀取行,直到找到起始字符串(或EOF)
設置一個標志,指示您正在搜索結束字符串
讀取行,直到找到結尾字符串(或EOF)
找到結束字符串時,設置標志

打印在開始字符串和結束字符串之間讀取的所有行

好了,您可以找到T | DataObject的第一個引用,確認它是您要查找的類型,然后尋找T | DataObject的下一個引用。

似乎只是一些簡單的字符串操作。

是所有這些都放在一行上還是對象標頭也由回車分隔?

更新
這絕不是最好的方法,而只是一種可能的方法:

String sRecords = "T|DataObject.Test|C|RecordHeaderId|D|123|D|234|T|DataObject.Test2|C|RecordHeaderId|D|2345|D2366";

// this will split the string into an array on the boundary of |.
// which means you'll have all of item individual items separated out.
String[] sArray = sRecords.Split('|');
List<String> objects = new List<string>();
String obj = String.Empty;

foreach (String s in sArray) {
    // locate the T item which defines a new record definition.  
    // the danger is if your data contains a "T" value somewhere it shouldn't
    if (s.Equals("T") && !String.IsNullOrEmpty(obj)) {
        objects.Add(obj);
        obj = String.Empty;
    }
    obj = String.Format("{0}|{1}", obj, s);
}
objects.Add(obj);

// at this point "objects" hold a list of strings, each defined by the record type.
foreach (String s in objects) {
    listBox1.Items.Add(s);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM