NepSyn14
NepSyn14

Reputation: 163

Year not displaying correctly with regex

Using regex to match this line of text I have discovered that the year is still appearing as 2015-01-07 and not just 2015. Can anyone see what's wrong with my 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 I not single out the 'year'? I ran it through regex101.com and here are the 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`

Upvotes: 1

Views: 53

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

You should move the parenthesis from ))) to (?:\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 with such a long regex and use comments like # Year to keep track of what you 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:

enter image description here

Upvotes: 2

vks
vks

Reputation: 67968

(?<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>.*)

                              ^^

Try this.See demo.You had missed a ) after year.

https://regex101.com/r/oF9hR9/16

Upvotes: 2

Related Questions