问题描述
大家好
我要读取此文本块
LXKM / AD310T0901E / IRNCPK1V53411100 / 006 13-08-19 03: 00:11
8917(7475)OMT-01 / ITMTAC 2893/06189
交通测量:TRUNK GROUP 13-08-17 00 :15
数据质量:安全
TGNO:VLX22 VLX27 VLX281 V100K
OPMODE / TGRPTYP:BW BW BW BW
可用性:
-------------------- + - -------- + ------- + ------- + ------- + ------- + -------
CC:I 3 59 1 50
TC:I(DERL)4 31 2 62
CCS与答案:I 4 40 1 35
TC答案:我(DERL) 3 30 2 47
LXKM / AD310T0901E / IRNCPK1V53411100 / 006 13-08-19 03:00:15
8917(7475)OMT-01 / ITMTAC 2893/06189
交通测量: TRUNK GROUP 13-08-17 00:15
数据质量:安全
TGNO:V7BSTD
OPMODE / TGRPTYP:BW
可用性:
-------------------- + --------- + ------- ------- + ------- + ------- + ------- +
CC:我7
TC:我(DERL)5
CCS与答案:我4
T C答案:我(DERL)3
这个文本有两个块。如何读取和分离这些块?
Hi all
I want read blocks of this text
LXKM/AD310T0901E/IRNCPK1V53411100/006 13-08-19 03:00:11
8917(7475) OMT-01/ITMTAC 2893/06189
TRAFFIC MEASUREMENT : TRUNK GROUP 13-08-17 00:15
DATA QUALITY : SECURE
TGNO : VLX22 VLX27 VLX281 V100K
OPMODE/TGRPTYP : BW BW BW BW
AVAILABILITY :
--------------------+---------+-------+-------+-------+-------+-------
CC:I 3 59 1 50
TC:I (DERL) 4 31 2 62
CCS WITH ANSWER:I 4 40 1 35
TC ANSWER:I (DERL) 3 30 2 47
LXKM/AD310T0901E/IRNCPK1V53411100/006 13-08-19 03:00:15
8917(7475) OMT-01/ITMTAC 2893/06189
TRAFFIC MEASUREMENT : TRUNK GROUP 13-08-17 00:15
DATA QUALITY : SECURE
TGNO : V7BSTD
OPMODE/TGRPTYP : BW
AVAILABILITY :
--------------------+---------+-------+-------+-------+-------+-------
CC:I 7
TC:I (DERL) 5
CCS WITH ANSWER:I 4
TC ANSWER:I (DERL) 3
this text has two blocks . How can I read and separate these blocks ?
推荐答案
// Assumption: The gap between the end of one block and the next is 4 carriage
// return, line feed pairs. If it isn't you'll have to work out the appropriate
// regex pattern for the delimiter.
const string DELIMITER = @"[\r\n]{4}";
// the data to split...
string content =
@"LXKM/AD310T0901E/IRNCPK1V53411100/006 13-08-19 03:00:11
8917(7475) OMT-01/ITMTAC 2893/06189
TRAFFIC MEASUREMENT : TRUNK GROUP 13-08-17 00:15
DATA QUALITY : SECURE
TGNO : VLX22 VLX27 VLX281 V100K
OPMODE/TGRPTYP : BW BW BW BW
AVAILABILITY :
--------------------+---------+-------+-------+-------+-------+-------
CC:I 3 59 1 50
TC:I (DERL) 4 31 2 62
CCS WITH ANSWER:I 4 40 1 35
TC ANSWER:I (DERL) 3 30 2 47
LXKM/AD310T0901E/IRNCPK1V53411100/006 13-08-19 03:00:15
8917(7475) OMT-01/ITMTAC 2893/06189
TRAFFIC MEASUREMENT : TRUNK GROUP 13-08-17 00:15
DATA QUALITY : SECURE
TGNO : V7BSTD
OPMODE/TGRPTYP : BW
AVAILABILITY :
--------------------+---------+-------+-------+-------+-------+-------
CC:I 7
TC:I (DERL) 5
CCS WITH ANSWER:I 4
TC ANSWER:I (DERL) 3 ";
// Now split on the basis of the assumed delimiter...
Regex chunk = new Regex(DELIMITER, RegexOptions.IgnoreCase|RegexOptions.Multiline);
// Using LINQ for the fun of it. A simple foreach would work equally well.
var getBlocks = from string s in chunk.Split(content)
where (!string.IsNullOrEmpty(s.Trim()))
select s.Trim();
foreach(string block in getBlocks)
{
Console.WriteLine(block);
Console.WriteLine(@"{0}///////{0}", Environment.NewLine);
}
这篇关于通过c#读取和分隔文本文件中的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!