我有一个文本文件,其内容如下:
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个空格)中的 block 数并保存元素数组 X 的 block 。结果应该是, 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();
我非常感谢任何线索和帮助。非常感谢您。
最佳答案
使用
(?<=initTest)(.|\n)*?(?=endTest)
在哪里
initTest(.|\n)*?endTest
将捕获所需的文本,但包括 initTest 和 endTest。使用 (?<=...) 和 (?=...) 将有助于摆脱它们。
关于c# - 使用正则表达式 C# 在两个字符串的 block 中获取文本文件的多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38774057/