我正在编写一些代码,这些代码将搜索txt文件并将其转换为其他格式。当我尝试从文本值中获取日期时,当我知道我的正则表达式工作正常时,它将返回null。
我曾尝试制作if语句来检查match.success。香港专业教育学院成功地将日期抓到一个列表中,但其他所有值都为null,这无济于事。
var ofd = new OpenFileDialog
{
Filter = "TRC File (*.trc*)|*.trc*",
Multiselect = true,
};
ofd.ShowDialog();
string path = ofd.FileName;
List<string> alllinesText = File.ReadAllLines(path).ToList();
string pattern = @"([0-9]{1,6}.\d).............\w(\w\w\w)\s\s...(\w\w.\w\w.\w\w.\w\w.\w\w.\w\w.\w\w.\w\w)";
string datepattern = @"[0-9]{1,2}.\d\d.\d\d\d\d\s\d\d.\d\d.\d\d.\d\d\d";
RegexOptions options = RegexOptions.Multiline;
List<string> all = new List<string>();
string info = File.ReadLines(path).ToString();
var date = Regex.Match(info, datepattern, options);
string datetime = Convert.ToString(date);
DateTime mydate = DateTime.ParseExact(datetime, "MM/dd/yyyy HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
foreach (string line in alllinesText)
{
foreach (Match m in Regex.Matches(line, pattern, options))
{
string timems = m.Groups[1].Value;
TimeSpan ms = TimeSpan.Parse(timems);
DateTime combined = mydate.Add(ms);
string finaltime = combined.ToString();
string matchid = m.Groups[2].Value;
string matchdata = m.Groups[3].Value;
matchdata = Regex.Replace(matchdata, @"\s+", "");
string iddata = finaltime + "," + matchid + "," + matchdata;
all.Add(iddata);
}
}
TextWriter tw = new StreamWriter(@"C:\Users\Public\test.txt");
foreach (string s in all)
{
tw.WriteLine(s);
}
tw.Close();
}
我正在尝试在文本文件的标题中获取开始日期。
;$FILEVERSION=1.1
;$STARTTIME=43626.4234913889
;
; C:\Users\acarducci\Desktop\chris trace.trc
;
; Start time: 6/10/2019 10:09:49.656.0
; Generated by PCAN-View v4.2.1.533
;
; Message Number
; | Time Offset (ms)
; | | Type
; | | | ID (hex)
; | | | | Data Length
; | | | | | Data Bytes (hex) ...
; | | | | | |
;---+-- ----+---- --+-- ----+--- + -+ -- -- -- -- -- -- --
1) 2.0 Rx 0400 8 01 5A 01 57 01 D2 A6 02
2) 8.6 Rx 0500 8 02 C1 02 C9 02 BE 02 C2
3) 36.2 Rx 0401 8 01 58 01 59 01 01 01 01