c# - Year not displaying correctly with regex -
using regex match line of text have discovered year still appearing 2015-01-07
, not 2015
. can see what's wrong regex?
line of code:
2015-01-07 wed jan 07 11:03:43.390 dd started
my regex:
(?<date>(?<year>(?:\d{4}|\d{2})-(?<month>\d{1,2})-(?<day>\d{1,2})))\s(?<logentry1>.*)\s(?<logentry2>.*)\s(?<logentry3>.*)\s(?<time>(?<hour>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2}).(?<milli>\d{0,3}))\s(?<logentry>.*)
why can not single out 'year'? ran through regex101.com , here capture group values:
match 1 date [0-10] `2015-01-07` year [0-10] `2015-01-07` month [5-7] `01` day [8-10] `07` logentry1 [11-14] `wed` logentry2 [15-18] `jan` logentry3 [19-21] `07` time [22-34] `11:03:43.390` hour [22-24] `11` minutes [25-27] `03` seconds [28-30] `43` milli [31-34] `390` logentry [35-45] `dd started`
you should move parenthesis )))
(?:\d{4}|\d{2}))
:
(?<date>(?<year>(?:\d{4}|\d{2}))-(?<month>\d{1,2})-(?<day>\d{1,2}))\s(?<logentry1>.*)\s(?<logentry2>.*)\s(?<logentry3>.*)\s(?<time>(?<hour>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2}).(?<milli>\d{0,3}))\s(?<logentry>.*)
actually, i'd rather use verbose regex option such long regex , use comments # year
keep track of have inside:
var rx = new regex(@"(?<date> (?<year> (?:\d{4}|\d{2}) ) # year - (?<month>\d{1,2}) - (?<day>\d{1,2}) ) # date \s (?<logentry1>.*) \s (?<logentry2>.*) \s (?<logentry3>.*) \s (?<time> (?<hour>\d{2}) : (?<minutes>\d{2}) : (?<seconds>\d{2}) . (?<milli>\d{0,3}) ) \s (?<logentry>.*)", regexoptions.ignorepatternwhitespace);
result:
Comments
Post a Comment