正则表达式C#的难度

时间:2015-04-29 10:45:03

标签: c# regex

我有一个来自文本文件的行,我正在尝试创建一个匹配的正则表达式。这是文本行。

    2015-01-07 Wed Jan 07 11:03:43.390 Some text here..

我要匹配的正则表达式如下:

    (?<date>(?<year>(?:\d{4}|\d{2})-(?<month>\d{1,2})-(?<day>\d{1,2})))\s(?<txtEntry1>.*)\s(?<txtEntry2>.*)\s(?<txtEntry3>.*)\s(?<time>(?<hour>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2}):(?<milli>\d{0,3}))\s(?<txtEntry4>.*)\s(?<txtEntry5>.*))

不匹配。我并不担心'措辞'日期 07年1月3日所以我刚刚把它作为文本条目,而不是匹配它到dd / mm / yy。我一直试图把它想象成我们但没有成功。谁能看到我出错的地方?

1 个答案:

答案 0 :(得分:1)

这对我有用:

(?<date>(?<year>(?:\d{4}|\d{2}))-(?<month>\d{1,2})-(?<day>\d{1,2}))\s(?<txtEntry1>\S*)\s(?<txtEntry2>\S*)\s(?<txtEntry3>\S*)\s(?<time>(?<hour>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})\.(?<milli>\d{0,3}))\s(?<txtEntry4>.*)

不确定你的textentry5虽然

Found 1 match:
2015-01-07 Wed Jan 07 11:03:43.390 Some text here.. has 13 groups:
2015-01-07 (date)
2015 (year)
01 (month)
07 (day)
Wed (txtEntry1)
Jan (txtEntry2)
07 (txtEntry3)
11:03:43.390 (time)
11 (hour)
03 (minutes)
43 (seconds)
390 (milli)
Some text here.. (txtEntry4)
String literals for use in programs:
C#
@"(?<date>(?<year>(?:\d{4}|\d{2}))-(?<month>\d{1,2})-(?<day>\d{1,2}))\s(?<txtEntry1>\S*)\s(?<txtEntry2>\S*)\s(?<txtEntry3>\S*)\s(?<time>(?<hour>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})\.(?<milli>\d{0,3}))\s(?<txtEntry4>.*)"