[英]How to read a specific line and text from a text file
string lot = "RU644276G01";
var year = "201" + lot.Substring(2, 1);
var folder = @"\\sinsdn38.ap.infineon.com\ArchView\03_Reports\" + year +
@"\" + lot.Substring(3, 2) + @"\" + lot.Substring(0,8) + @"\";
DirectoryInfo di = new DirectoryInfo(folder);
foreach (var fi in di.GetFiles("*.TLT"))
{
var file = fi.FullName;
string line;
using (StreamReader sr = new StreamReader(file))
{
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith("TEST-END"))
{
timeStampTextBox.Text = line;
}
}
}
This is my code currently. 这是我目前的代码。
I want to read from a specific line (for example line 8) and the line starts with "Test-End"
. 我想从特定的行(例如第8行)中读取内容,并且该行以
"Test-End"
开头。 However, line 8 contains all these 但是,第8行包含所有这些内容
"TEST-END : 2017-01-08 15:51 PROGRAM : TLE8888QK-B2 BAU-NR : 95187193"
but I only want to read "2017-01-98 15:51"
. 但我只想阅读
"2017-01-98 15:51"
。
How do I change my code to get that? 我该如何更改我的代码呢? Currently I'm getting the whole line instead of the specific timestamp that I want.
目前,我正在获取整行而不是所需的特定时间戳。
Edit How do I change the code such that the string lot =" "
can be any number, meaning it does not need to be RU644276G01, it can be a different number which will be typed by users. 编辑如何更改代码,使
string lot =" "
可以是任何数字,这意味着它不必是RU644276G01,它可以是将由用户键入的其他数字。 I have created a textbox for users to input the number. 我创建了一个文本框供用户输入数字。
You extract the text. 您提取文本。 It seems quite regular pattern, so regular expressions should be able to help:
似乎是很规则的模式,所以正则表达式应该可以帮助:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
var line = "TEST-END : 2017-01-08 15:51 PROGRAM : TLE8888QK-B2 BAU-NR : 95187193";
Regex re = new Regex(@"^(?:TEST-END : )(.*?\d{4}-\d{2}-\d{2} \d{2}:\d{2})");
var match = re.Match(line);
Console.WriteLine(match.Groups[1]);
Console.ReadLine(); // leave console open
}
}
Output: 输出:
2017-01-08 15:51 // this is group 1, group 0 is the full capture including TEST-END :
Use this to check it in regexr: https://regexr.com/3l1sf if you hover about the text it will diplay your capturing groups 使用它在regexr中检查它: https ://regexr.com/3l1sf如果您将鼠标悬停在文本上,它将使您的捕获组不显示
The regex means: 正则表达式表示:
^ start of the string
(?:TEST-END : ) non capturing group, text must be present
( a group
.*? as few (0-n) anythings as possible
\d{4}-\d{2}-\d{2} \d{2}:\d{2} 4 digits-2 digits-2digits 2digits:2digits
) end of group
More about regular expressions: 有关正则表达式的更多信息:
a regex Tester (one of many, the one I use): https://regexr.com/ 一个正则表达式测试器(我使用的众多之一): https : //regexr.com/
you can split the line with ":", like this 您可以使用“:”分隔行,像这样
var value = line.split(':');
and get your date like this. 并得到你这样的约会。
var date = value[1] + ":" + value[2].Replace("PROGRAM", "");
above statement means 以上声明意味着
date = "2017-01-98 15" + ":" + "51"
if (line.StartsWith("TEST-END"))
{
var value = line.split(':');
var date = value[1] + ":" + value[2].Replace("PROGRAM", "");
timeStampTextBox.Text = date;
}
This is not the best answer, it depends on exactly the statement you had given. 这不是最佳答案,这完全取决于您给出的说明。
Here is my answer using Regular Expressions. 这是我使用正则表达式的答案。
if (line.StartsWith("TEST-END"))
{
Regex re = new Regex(@"\d{4}-\d{2}-\d{2} \d{2}:\d{2}");
var match = re.Match(line);
if(m.Success)
{
timeStampTextBox.Text = match.Value;
}
}
Output: 2017-01-08 15:51 输出:2017-01-08 15:51
I finally got all three parameters out of the last line 我终于从最后一行得到了所有三个参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
string pattern = @"(?'name'[^\s]+)\s:\s(?'value'[\w\s\-]*|\d{4}-\d{2}-\d{2}\s\d{2}:\d{2})";
string line = "TEST-END : 2017-01-08 15:51 PROGRAM : TLE8888QK-B2 BAU-NR : 95187193";
MatchCollection matches = Regex.Matches(line, pattern, RegexOptions.RightToLeft);
foreach (Match match in matches)
{
Console.WriteLine("name : '{0}', value : '{1}'", match.Groups["name"].Value, match.Groups["value"].Value);
dict.Add(match.Groups["name"].Value, match.Groups["value"].Value);
}
DateTime date = DateTime.Parse(dict["TEST-END"]);
Console.ReadLine();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.