简体   繁体   English

正则表达式

[英]Regular expression for this

I have a text file and I would like to parse it using regular expression. 我有一个文本文件,我想用正则表达式解析它。 how can i extract the text block between "Entry #" to the empty line before the next "Entry #" 如何在下一个“Entry#”之前将“Entry#”之间的文本块提取到空行

GmtOffset=120
GmtExistFlag=0
LocalTimeFlag=0
Entry #1
EventType=1
FieldType=256
FieldValue=12-05-2010, 11:00:00
FieldType=512
FieldValue=12-05-2010, 11:30:00
FieldType=1
FieldValue(3)=Jku

Entry #2
EventType=1
FieldType=256
FieldValue=15-05-2010, 06:00:00
FieldType=512
FieldValue=15-05-2010, 06:30:00
FieldType=1
FieldValue(3)=Lsh
FieldType=1024
FieldValue=15-05-2010, 05:45:00
FieldType=65536
FieldValue=1

Entry #3
EventType=4
FieldType=1
FieldValue(4)=STYL
FieldType=1024
FieldValue=13-05-2010, 11:00:00
FieldType=65536
FieldValue=1
FieldType=2097152
FieldValue=2
FieldType=8388608
FieldValue=-2147483648

How to ? 如何 ?

Thanks 谢谢

Splitting by the Entry # will give you what you want. 通过Entry # 分割将为您提供所需的内容。 Regexes are not necessary here. 这里没有必要使用正则表达式。 Just do split and remove empty lines on the end: 只需拆分并删除末尾的空行:

var blocks = text.Split("Entry #");
foreach (var block in blocks)
{
    // removing the line with the entry number
    block = block.Substring(block.IndexOf(Environment.NewLine));

    // removing the empty lines
    block = block.Trim('\n', '\r');

    // add your processing here
}

While I concur with the solution by @ie, I think that solution will overlook the problem of the number following the Entry # . 虽然我同意@ie的解决方案,但我认为该解决方案将忽略Entry #后面的数字问题。 In that case Regex.Split will work. 在这种情况下,Regex.Split将起作用。

string[]  matches = Regex.Split(inputStrng, @"Entry #\d+\s+");
foreach (string match in matches)
{     
    Console.WriteLine(match);
}

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

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