Both the below cases are not working.
(以下两种情况均无效。)
I want to extract from a text file a certain part that I can choose by specifying the start of the line and end. (我想从文本文件中提取可以通过指定行的开头和结尾来选择的特定部分。)
- case looks like this:
(情况如下:)
using (StreamReader reader = new StreamReader("C:/Users/david/Desktop/20180820.log",Encoding.Default))
{
Console.WriteLine("From:");
string a = (Console.ReadLine());
Console.WriteLine(" To:");
string b = (Console.ReadLine());
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (line.StartsWith(a) && (line.EndsWith(b)))
{
Console.WriteLine(line);
}
}
}
- case with regex
(正则表达式)
string line;
while ((line = reader.ReadLine()) != null)
{
string regex12 = a.ToString() + b.ToString();
Match m = Regex.Match(line,regex12);
string s = Regex.Match(line, regex12).Groups[0].Value;
Console.WriteLine(s);
if (m.Success)
{
string n = m.Groups[0].Value;
Console.WriteLine(n);
}
}
If anyone can solve my problem, I will be very thankful.
(如果任何人都能解决我的问题,我将非常感激。)
ask by David translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…