使用正则表达式C#在两个字符串的块内获取多行文本文件串

时间:2016-08-04 17:42:39

标签: c# regex

我有一个文本文件,其内容如下:

   initTest   
1234 567 8910
1234 567 8910
   endTest   

   initTest   
1234 567 8911
1234 567 8911
   endTest   

   initTest   
1234 567 8912
1234 567 8912
   endTest   

然后我需要获取“initTest”中的块数(在单词之前和之后有3个空格)和“endTest”(单词之前和之后有3个空格)并保存元素块到数组X.结果应该是, X [0] = {“1234 567 8910 \ n 1234 567 8910”} 和X.length = 3。

我尝试使用Regex在C#中使用代码,但结果是不匹配。

string text = line;
string search = @"(^\s*initTest.*?^\s*endTest)";

MatchCollection matches = Regex.Matches(text, search, RegexOptions.Singleline | RegexOptions.IgnoreCase);

Console.WriteLine("there was {0} matches for '{1}'", matches.Count, search);

Console.ReadLine();

我非常感谢任何线索和帮助。非常感谢你。

4 个答案:

答案 0 :(得分:3)

使用

initTest(.|\n)*?endTest

,其中

descdist()

将捕获所需的文本,但包括initTest和endTest。使用(?< = ...)和(?= ...)将有助于摆脱它们。

演示:https://dotnetfiddle.net/tiXRut

答案 1 :(得分:0)

试试这个正则表达式:

var text = @"
   initTest   
1234 567 8910
1234 567 8910
   endTest   

   initTest   
1234 567 8911
1234 567 8911
   endTest   

   initTest   
1234 567 8912
1234 567 8912
   endTest   
";

var pattern = string.Join(@"\s+", 
    @"\s+initTest",
    @"(?<sequence1>\d{4} \d{3} \d{4})",
    @"(?<sequence2>\d{4} \d{3} \d{4})",
    @"endTest");
var matches = Regex.Matches(text, pattern, RegexOptions.Multiline)
    .Cast<Match>()
    .Select(x => new
    {
        Content = x.Value,
        Sequence1 = x.Groups["sequence1"].Value,
        Sequence2 = x.Groups["sequence1"].Value,
    });

答案 2 :(得分:0)

void Main()
{
    string search = @"(?<=initTest)(.|\n)*?(?=endTest)";
    string text = GetData();

    MatchCollection matches = Regex.Matches(text, search, RegexOptions.Singleline | RegexOptions.IgnoreCase);

    Console.WriteLine("there were {0} matches for '{1}'", matches.Count, search);

    for(int i=0; i < matches.Count; i++)
        Console.WriteLine(matches[i].Groups[0].ToString());

    Console.ReadLine();
}

public string GetData()
{
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("   initTest");
    sb.AppendLine("1234 567 8910");
    sb.AppendLine("1234 567 8910");
    sb.AppendLine("   endTest");

    sb.AppendLine("   initTest");
    sb.AppendLine("1234 567 8911");
    sb.AppendLine("1234 567 8911");
    sb.AppendLine("   endTest");
    sb.AppendLine(" ");
    sb.AppendLine("   initTest");
    sb.AppendLine("1234 567 8912");
    sb.AppendLine("1234 567 8912");
    sb.AppendLine("   endTest");

    return sb.ToString();   
}

答案 3 :(得分:0)

如果你想在没有正则表达式的情况下这样做,你可以尝试这个解决方案:

class Program
{
    static void Main(string[] args)
    {
        string path = @"C:\Projects\StackOverRegX\StackOverRegX\input.txt";
        string[] x = new string[100];
        int index = 0;
        if (File.Exists(path))
        {
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    if(s.Contains("initTest"))
                    {
                        x[index] = sr.ReadLine() + " \n " + sr.ReadLine();
                        index++;
                    }
                }
            }
        }
        for (int i = 0; i < 100; i++)
        {
            if(x[i]!=null)
            Console.WriteLine(x[i]);
        }
        Console.ReadKey();
    }
}