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

3

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

   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。

我已经尝试使用C#中的Regex进行编码,但结果没有匹配项。

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();

非常感谢任何线索和帮助。非常感谢您的提前。


在运行正则表达式测试之前,text的内容是什么? - Jasen
@Jasen,实际上上面的文本并不是真实情况,它只是真实情况的简化版。我拥有的真实文本文件内容是多行字符串。如果答案不清楚,请告诉我。 - Cas
4个回答

3
使用
(?<=initTest)(.|\n)*?(?=endTest)

在哪里

initTest(.|\n)*?endTest

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

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


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,
    });

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();   
}

谢谢,你的模式与Smirnov给出的模式相同,是的,那就是解决方案。 - Cas

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();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接